pci.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Stand alone funtions for QSpan Tundra support.
  2. */
  3. #include <linux/types.h>
  4. #include <linux/pci.h>
  5. #include <asm/mpc8xx.h>
  6. extern void puthex(unsigned long val);
  7. extern void puts(const char *);
  8. /* To map PCI devices, you first write 0xffffffff into the device
  9. * base address registers. When the register is read back, the
  10. * number of most significant '1' bits describes the amount of address
  11. * space needed for mapping. If the most significant bit is not set,
  12. * either the device does not use that address register, or it has
  13. * a fixed address that we can't change. After the address is assigned,
  14. * the command register has to be written to enable the card.
  15. */
  16. typedef struct {
  17. u_char pci_bus;
  18. u_char pci_devfn;
  19. ushort pci_command;
  20. uint pci_addrs[6];
  21. } pci_map_t;
  22. /* We should probably dynamically allocate these structures.
  23. */
  24. #define MAX_PCI_DEVS 32
  25. int pci_dev_cnt;
  26. pci_map_t pci_map[MAX_PCI_DEVS];
  27. void pci_conf_write(int bus, int device, int func, int reg, uint writeval);
  28. void pci_conf_read(int bus, int device, int func, int reg, void *readval);
  29. void probe_addresses(int bus, int devfn);
  30. void map_pci_addrs(void);
  31. extern int
  32. qs_pci_read_config_byte(unsigned char bus, unsigned char dev_fn,
  33. unsigned char offset, unsigned char *val);
  34. extern int
  35. qs_pci_read_config_word(unsigned char bus, unsigned char dev_fn,
  36. unsigned char offset, unsigned short *val);
  37. extern int
  38. qs_pci_read_config_dword(unsigned char bus, unsigned char dev_fn,
  39. unsigned char offset, unsigned int *val);
  40. extern int
  41. qs_pci_write_config_byte(unsigned char bus, unsigned char dev_fn,
  42. unsigned char offset, unsigned char val);
  43. extern int
  44. qs_pci_write_config_word(unsigned char bus, unsigned char dev_fn,
  45. unsigned char offset, unsigned short val);
  46. extern int
  47. qs_pci_write_config_dword(unsigned char bus, unsigned char dev_fn,
  48. unsigned char offset, unsigned int val);
  49. /* This is a really stripped version of PCI bus scan. All we are
  50. * looking for are devices that exist.
  51. */
  52. void
  53. pci_scanner(int addr_probe)
  54. {
  55. unsigned int devfn, l, class, bus_number;
  56. unsigned char hdr_type, is_multi;
  57. is_multi = 0;
  58. bus_number = 0;
  59. for (devfn = 0; devfn < 0xff; ++devfn) {
  60. /* The device numbers are comprised of upper 5 bits of
  61. * device number and lower 3 bits of multi-function number.
  62. */
  63. if ((devfn & 7) && !is_multi) {
  64. /* Don't scan multifunction addresses if this is
  65. * not a multifunction device.
  66. */
  67. continue;
  68. }
  69. /* Read the header to determine card type.
  70. */
  71. qs_pci_read_config_byte(bus_number, devfn, PCI_HEADER_TYPE,
  72. &hdr_type);
  73. /* If this is a base device number, check the header to
  74. * determine if it is mulifunction.
  75. */
  76. if ((devfn & 7) == 0)
  77. is_multi = hdr_type & 0x80;
  78. /* Check to see if the board is really in the slot.
  79. */
  80. qs_pci_read_config_dword(bus_number, devfn, PCI_VENDOR_ID, &l);
  81. /* some broken boards return 0 if a slot is empty: */
  82. if (l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff ||
  83. l == 0xffff0000) {
  84. /* Nothing there.
  85. */
  86. is_multi = 0;
  87. continue;
  88. }
  89. /* If we are not performing an address probe,
  90. * just simply print out some information.
  91. */
  92. if (!addr_probe) {
  93. qs_pci_read_config_dword(bus_number, devfn,
  94. PCI_CLASS_REVISION, &class);
  95. class >>= 8; /* upper 3 bytes */
  96. #if 0
  97. printf("Found (%3d:%d): vendor 0x%04x, device 0x%04x, class 0x%06x\n",
  98. (devfn >> 3), (devfn & 7),
  99. (l & 0xffff), (l >> 16) & 0xffff, class);
  100. #else
  101. puts("Found ("); puthex(devfn >> 3);
  102. puts(":"); puthex(devfn & 7);
  103. puts("): vendor "); puthex(l & 0xffff);
  104. puts(", device "); puthex((l >> 16) & 0xffff);
  105. puts(", class "); puthex(class); puts("\n");
  106. #endif
  107. }
  108. else {
  109. /* If this is a "normal" device, build address list.
  110. */
  111. if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_NORMAL)
  112. probe_addresses(bus_number, devfn);
  113. }
  114. }
  115. /* Now map the boards.
  116. */
  117. if (addr_probe)
  118. map_pci_addrs();
  119. }
  120. /* Probe addresses for the specified device. This is a destructive
  121. * operation because it writes the registers.
  122. */
  123. void
  124. probe_addresses(bus, devfn)
  125. {
  126. int i;
  127. uint pciaddr;
  128. ushort pcicmd;
  129. pci_map_t *pm;
  130. if (pci_dev_cnt >= MAX_PCI_DEVS) {
  131. puts("Too many PCI devices\n");
  132. return;
  133. }
  134. pm = &pci_map[pci_dev_cnt++];
  135. pm->pci_bus = bus;
  136. pm->pci_devfn = devfn;
  137. for (i=0; i<6; i++) {
  138. qs_pci_write_config_dword(bus, devfn, PCI_BASE_ADDRESS_0 + (i * 4), -1);
  139. qs_pci_read_config_dword(bus, devfn, PCI_BASE_ADDRESS_0 + (i * 4),
  140. &pciaddr);
  141. pm->pci_addrs[i] = pciaddr;
  142. qs_pci_read_config_word(bus, devfn, PCI_COMMAND, &pcicmd);
  143. pm->pci_command = pcicmd;
  144. }
  145. }
  146. /* Map the cards into the PCI space. The PCI has separate memory
  147. * and I/O spaces. In addition, some memory devices require mapping
  148. * below 1M. The least significant 4 bits of the address register
  149. * provide information. If this is an I/O device, only the LS bit
  150. * is used to indicate that, so I/O devices can be mapped to a two byte
  151. * boundard. Memory addresses can be mapped to a 32 byte boundary.
  152. * The QSpan implementations usually have a 1Gbyte space for each
  153. * memory and I/O spaces.
  154. *
  155. * This isn't a terribly fancy algorithm. I just map the spaces from
  156. * the top starting with the largest address space. When finished,
  157. * the registers are written and the card enabled.
  158. *
  159. * While the Tundra can map a large address space on most boards, we
  160. * need to be careful because it may overlap other devices (like IMMR).
  161. */
  162. #define MEMORY_SPACE_SIZE 0x20000000
  163. #define IO_SPACE_SIZE 0x20000000
  164. void
  165. map_pci_addrs()
  166. {
  167. uint pci_mem_top, pci_mem_low;
  168. uint pci_io_top;
  169. uint addr_mask, reg_addr, space;
  170. int i, j;
  171. pci_map_t *pm;
  172. pci_mem_top = MEMORY_SPACE_SIZE;
  173. pci_io_top = IO_SPACE_SIZE;
  174. pci_mem_low = (1 * 1024 * 1024); /* Below one meg addresses */
  175. /* We can't map anything more than the maximum space, but test
  176. * for it anyway to catch devices out of range.
  177. */
  178. addr_mask = 0x80000000;
  179. do {
  180. space = (~addr_mask) + 1; /* Size of the space */
  181. for (i=0; i<pci_dev_cnt; i++) {
  182. pm = &pci_map[i];
  183. for (j=0; j<6; j++) {
  184. /* If the MS bit is not set, this has either
  185. * already been mapped, or is not used.
  186. */
  187. reg_addr = pm->pci_addrs[j];
  188. if ((reg_addr & 0x80000000) == 0)
  189. continue;
  190. if (reg_addr & PCI_BASE_ADDRESS_SPACE_IO) {
  191. if ((reg_addr & PCI_BASE_ADDRESS_IO_MASK) != addr_mask)
  192. continue;
  193. if (pci_io_top < space) {
  194. puts("Out of PCI I/O space\n");
  195. }
  196. else {
  197. pci_io_top -= space;
  198. pm->pci_addrs[j] = pci_io_top;
  199. pm->pci_command |= PCI_COMMAND_IO;
  200. }
  201. }
  202. else {
  203. if ((reg_addr & PCI_BASE_ADDRESS_MEM_MASK) != addr_mask)
  204. continue;
  205. /* Memory space. Test if below 1M.
  206. */
  207. if (reg_addr & PCI_BASE_ADDRESS_MEM_TYPE_1M) {
  208. if (pci_mem_low < space) {
  209. puts("Out of PCI 1M space\n");
  210. }
  211. else {
  212. pci_mem_low -= space;
  213. pm->pci_addrs[j] = pci_mem_low;
  214. }
  215. }
  216. else {
  217. if (pci_mem_top < space) {
  218. puts("Out of PCI Mem space\n");
  219. }
  220. else {
  221. pci_mem_top -= space;
  222. pm->pci_addrs[j] = pci_mem_top;
  223. }
  224. }
  225. pm->pci_command |= PCI_COMMAND_MEMORY;
  226. }
  227. }
  228. }
  229. addr_mask >>= 1;
  230. addr_mask |= 0x80000000;
  231. } while (addr_mask != 0xfffffffe);
  232. /* Now, run the list one more time and map everything.
  233. */
  234. for (i=0; i<pci_dev_cnt; i++) {
  235. pm = &pci_map[i];
  236. for (j=0; j<6; j++) {
  237. qs_pci_write_config_dword(pm->pci_bus, pm->pci_devfn,
  238. PCI_BASE_ADDRESS_0 + (j * 4), pm->pci_addrs[j]);
  239. }
  240. /* Enable memory or address mapping.
  241. */
  242. qs_pci_write_config_word(pm->pci_bus, pm->pci_devfn, PCI_COMMAND,
  243. pm->pci_command);
  244. }
  245. }