qspan_pci.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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