qspan_pci.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * LinuxPPC arch/ppc/kernel/qspan_pci.c Dan Malek (dmalek@jlc.net)
  3. *
  4. * QSpan Motorola bus to PCI bridge. The config address register
  5. * is located 0x500 from the base of the bridge control/status registers.
  6. * The data register is located at 0x504.
  7. * This is a two step operation. First, the address register is written,
  8. * then the data register is read/written as required.
  9. * I don't know what to do about interrupts (yet).
  10. */
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <asm/mpc8xx.h>
  15. /*
  16. * When reading the configuration space, if something does not respond
  17. * the bus times out and we get a machine check interrupt. So, the
  18. * good ol' exception tables come to mind to trap it and return some
  19. * value.
  20. *
  21. * On an error we just return a -1, since that is what the caller wants
  22. * returned if nothing is present. I copied this from __get_user_asm,
  23. * with the only difference of returning -1 instead of EFAULT.
  24. * There is an associated hack in the machine check trap code.
  25. *
  26. * The QSPAN is also a big endian device, that is it makes the PCI
  27. * look big endian to us. This presents a problem for the Linux PCI
  28. * functions, which assume little endian. For example, we see the
  29. * first 32-bit word like this:
  30. * ------------------------
  31. * | Device ID | Vendor ID |
  32. * ------------------------
  33. * If we read/write as a double word, that's OK. But in our world,
  34. * when read as a word, device ID is at location 0, not location 2 as
  35. * the little endian PCI would believe. We have to switch bits in
  36. * the PCI addresses given to us to get the data to/from the correct
  37. * byte lanes.
  38. *
  39. * The QSPAN only supports 4 bits of "slot" in the dev_fn instead of 5.
  40. * It always forces the MS bit to zero. Therefore, dev_fn values
  41. * greater than 128 are returned as "no device found" errors.
  42. *
  43. * The QSPAN can only perform long word (32-bit) configuration cycles.
  44. * The "offset" must have the two LS bits set to zero. Read operations
  45. * require we read the entire word and then sort out what should be
  46. * returned. Write operations other than long word require that we
  47. * read the long word, update the proper word or byte, then write the
  48. * entire long word back.
  49. *
  50. * PCI Bridge hack. We assume (correctly) that bus 0 is the primary
  51. * PCI bus from the QSPAN. If we are called with a bus number other
  52. * than zero, we create a Type 1 configuration access that a downstream
  53. * PCI bridge will interpret.
  54. */
  55. #define __get_pci_config(x, addr, op) \
  56. __asm__ __volatile__( \
  57. "1: "op" %0,0(%1)\n" \
  58. " eieio\n" \
  59. "2:\n" \
  60. ".section .fixup,\"ax\"\n" \
  61. "3: li %0,-1\n" \
  62. " b 2b\n" \
  63. ".section __ex_table,\"a\"\n" \
  64. " .align 2\n" \
  65. " .long 1b,3b\n" \
  66. ".text" \
  67. : "=r"(x) : "r"(addr))
  68. #define QS_CONFIG_ADDR ((volatile uint *)(PCI_CSR_ADDR + 0x500))
  69. #define QS_CONFIG_DATA ((volatile uint *)(PCI_CSR_ADDR + 0x504))
  70. #define mk_config_addr(bus, dev, offset) \
  71. (((bus)<<16) | ((dev)<<8) | (offset & 0xfc))
  72. #define mk_config_type1(bus, dev, offset) \
  73. mk_config_addr(bus, dev, offset) | 1;
  74. /* Initialize the QSpan device registers after power up.
  75. */
  76. void
  77. qspan_init(void)
  78. {
  79. uint *qptr;
  80. qptr = (uint *)PCI_CSR_ADDR;
  81. /* PCI Configuration/status. Upper bits written to clear
  82. * pending interrupt or status. Lower bits enable QSPAN as
  83. * PCI master, enable memory and I/O cycles, and enable PCI
  84. * parity error checking.
  85. * IMPORTANT: The last two bits of this word enable PCI
  86. * master cycles into the QBus. The QSpan is broken and can't
  87. * meet the timing specs of the PQ bus for this to work. Therefore,
  88. * if you don't have external bus arbitration, you can't use
  89. * this function.
  90. */
  91. #ifdef EXTERNAL_PQ_ARB
  92. qptr[1] = 0xf9000147;
  93. #else
  94. qptr[1] = 0xf9000144;
  95. #endif
  96. /* PCI Misc configuration. Set PCI latency timer resolution
  97. * of 8 cycles, set cache size to 4 x 32.
  98. */
  99. qptr[3] = 0;
  100. /* Set up PCI Target address mapping. Enable, Posted writes,
  101. * 2Gbyte space (processor memory controller determines actual size).
  102. */
  103. qptr[64] = 0x8f000080;
  104. /* Map processor 0x80000000 to PCI 0x00000000.
  105. * Processor address bit 1 determines I/O type access (0x80000000)
  106. * or memory type access (0xc0000000).
  107. */
  108. qptr[65] = 0x80000000;
  109. /* Enable error logging and clear any pending error status.
  110. */
  111. qptr[80] = 0x90000000;
  112. qptr[512] = 0x000c0003;
  113. /* Set up Qbus slave image.
  114. */
  115. qptr[960] = 0x01000000;
  116. qptr[961] = 0x000000d1;
  117. qptr[964] = 0x00000000;
  118. qptr[965] = 0x000000d1;
  119. }
  120. /* Functions to support PCI bios-like features to read/write configuration
  121. * space. If the function fails for any reason, a -1 (0xffffffff) value
  122. * must be returned.
  123. */
  124. #define DEVICE_NOT_FOUND (-1)
  125. #define SUCCESSFUL 0
  126. int qs_pci_read_config_byte(unsigned char bus, unsigned char dev_fn,
  127. unsigned char offset, unsigned char *val)
  128. {
  129. uint temp;
  130. u_char *cp;
  131. if ((bus > 7) || (dev_fn > 127)) {
  132. *val = 0xff;
  133. return DEVICE_NOT_FOUND;
  134. }
  135. if (bus == 0)
  136. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  137. else
  138. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  139. __get_pci_config(temp, QS_CONFIG_DATA, "lwz");
  140. offset ^= 0x03;
  141. cp = ((u_char *)&temp) + (offset & 0x03);
  142. *val = *cp;
  143. return SUCCESSFUL;
  144. }
  145. int qs_pci_read_config_word(unsigned char bus, unsigned char dev_fn,
  146. unsigned char offset, unsigned short *val)
  147. {
  148. uint temp;
  149. ushort *sp;
  150. if ((bus > 7) || (dev_fn > 127)) {
  151. *val = 0xffff;
  152. return DEVICE_NOT_FOUND;
  153. }
  154. if (bus == 0)
  155. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  156. else
  157. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  158. __get_pci_config(temp, QS_CONFIG_DATA, "lwz");
  159. offset ^= 0x02;
  160. sp = ((ushort *)&temp) + ((offset >> 1) & 1);
  161. *val = *sp;
  162. return SUCCESSFUL;
  163. }
  164. int qs_pci_read_config_dword(unsigned char bus, unsigned char dev_fn,
  165. unsigned char offset, unsigned int *val)
  166. {
  167. if ((bus > 7) || (dev_fn > 127)) {
  168. *val = 0xffffffff;
  169. return DEVICE_NOT_FOUND;
  170. }
  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_pci_config(*val, QS_CONFIG_DATA, "lwz");
  176. return SUCCESSFUL;
  177. }
  178. int qs_pci_write_config_byte(unsigned char bus, unsigned char dev_fn,
  179. unsigned char offset, unsigned char val)
  180. {
  181. uint temp;
  182. u_char *cp;
  183. if ((bus > 7) || (dev_fn > 127))
  184. return DEVICE_NOT_FOUND;
  185. qs_pci_read_config_dword(bus, dev_fn, offset, &temp);
  186. offset ^= 0x03;
  187. cp = ((u_char *)&temp) + (offset & 0x03);
  188. *cp = val;
  189. if (bus == 0)
  190. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  191. else
  192. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  193. *QS_CONFIG_DATA = temp;
  194. return SUCCESSFUL;
  195. }
  196. int qs_pci_write_config_word(unsigned char bus, unsigned char dev_fn,
  197. unsigned char offset, unsigned short val)
  198. {
  199. uint temp;
  200. ushort *sp;
  201. if ((bus > 7) || (dev_fn > 127))
  202. return DEVICE_NOT_FOUND;
  203. qs_pci_read_config_dword(bus, dev_fn, offset, &temp);
  204. offset ^= 0x02;
  205. sp = ((ushort *)&temp) + ((offset >> 1) & 1);
  206. *sp = val;
  207. if (bus == 0)
  208. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  209. else
  210. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  211. *QS_CONFIG_DATA = temp;
  212. return SUCCESSFUL;
  213. }
  214. int qs_pci_write_config_dword(unsigned char bus, unsigned char dev_fn,
  215. unsigned char offset, unsigned int val)
  216. {
  217. if ((bus > 7) || (dev_fn > 127))
  218. return DEVICE_NOT_FOUND;
  219. if (bus == 0)
  220. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  221. else
  222. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  223. *(unsigned int *)QS_CONFIG_DATA = val;
  224. return SUCCESSFUL;
  225. }