pci_ftpci100.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Faraday FTPCI100 PCI Bridge Controller Device Driver Implementation
  3. *
  4. * Copyright (C) 2011 Andes Technology Corporation
  5. * Gavin Guo, Andes Technology Corporation <gavinguo@andestech.com>
  6. * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <pci.h>
  13. #include <faraday/ftpci100.h>
  14. #include <asm/io.h>
  15. #include <asm/types.h> /* u32, u16.... used by pci.h */
  16. struct ftpci100_data {
  17. unsigned int reg_base;
  18. unsigned int io_base;
  19. unsigned int mem_base;
  20. unsigned int mmio_base;
  21. unsigned int ndevs;
  22. };
  23. static struct pci_config devs[FTPCI100_MAX_FUNCTIONS];
  24. static struct pci_controller local_hose;
  25. static void setup_pci_bar(unsigned int bus, unsigned int dev, unsigned func,
  26. unsigned char header, struct ftpci100_data *priv)
  27. {
  28. struct pci_controller *hose = (struct pci_controller *)&local_hose;
  29. unsigned int i, tmp32, bar_no, iovsmem = 1;
  30. pci_dev_t dev_nu;
  31. /* A device is present, add an entry to the array */
  32. devs[priv->ndevs].bus = bus;
  33. devs[priv->ndevs].dev = dev;
  34. devs[priv->ndevs].func = func;
  35. dev_nu = PCI_BDF(bus, dev, func);
  36. if ((header & 0x7f) == 0x01)
  37. /* PCI-PCI Bridge */
  38. bar_no = 2;
  39. else
  40. bar_no = 6;
  41. /* Allocate address spaces by configuring BARs */
  42. for (i = 0; i < bar_no; i++) {
  43. pci_hose_write_config_dword(hose, dev_nu,
  44. PCI_BASE_ADDRESS_0 + i * 4, 0xffffffff);
  45. pci_hose_read_config_dword(hose, dev_nu,
  46. PCI_BASE_ADDRESS_0 + i * 4, &tmp32);
  47. if (tmp32 == 0x0)
  48. continue;
  49. /* IO space */
  50. if (tmp32 & 0x1) {
  51. iovsmem = 0;
  52. unsigned int size_mask = ~(tmp32 & 0xfffffffc);
  53. if (priv->io_base & size_mask)
  54. priv->io_base = (priv->io_base & ~size_mask) + \
  55. size_mask + 1;
  56. devs[priv->ndevs].bar[i].addr = priv->io_base;
  57. devs[priv->ndevs].bar[i].size = size_mask + 1;
  58. pci_hose_write_config_dword(hose, dev_nu,
  59. PCI_BASE_ADDRESS_0 + i * 4,
  60. priv->io_base);
  61. debug("Allocated IO address 0x%X-" \
  62. "0x%X for Bus %d, Device %d, Function %d\n",
  63. priv->io_base,
  64. priv->io_base + size_mask, bus, dev, func);
  65. priv->io_base += size_mask + 1;
  66. } else {
  67. /* Memory space */
  68. unsigned int is_64bit = ((tmp32 & 0x6) == 0x4);
  69. unsigned int is_pref = tmp32 & 0x8;
  70. unsigned int size_mask = ~(tmp32 & 0xfffffff0);
  71. unsigned int alloc_base;
  72. unsigned int *addr_mem_base;
  73. if (is_pref)
  74. addr_mem_base = &priv->mem_base;
  75. else
  76. addr_mem_base = &priv->mmio_base;
  77. alloc_base = *addr_mem_base;
  78. if (alloc_base & size_mask)
  79. alloc_base = (alloc_base & ~size_mask) \
  80. + size_mask + 1;
  81. pci_hose_write_config_dword(hose, dev_nu,
  82. PCI_BASE_ADDRESS_0 + i * 4, alloc_base);
  83. debug("Allocated %s address 0x%X-" \
  84. "0x%X for Bus %d, Device %d, Function %d\n",
  85. is_pref ? "MEM" : "MMIO", alloc_base,
  86. alloc_base + size_mask, bus, dev, func);
  87. devs[priv->ndevs].bar[i].addr = alloc_base;
  88. devs[priv->ndevs].bar[i].size = size_mask + 1;
  89. debug("BAR address BAR size\n");
  90. debug("%010x %08d\n",
  91. devs[priv->ndevs].bar[0].addr,
  92. devs[priv->ndevs].bar[0].size);
  93. alloc_base += size_mask + 1;
  94. *addr_mem_base = alloc_base;
  95. if (is_64bit) {
  96. i++;
  97. pci_hose_write_config_dword(hose, dev_nu,
  98. PCI_BASE_ADDRESS_0 + i * 4, 0x0);
  99. }
  100. }
  101. }
  102. /* Enable Bus Master, Memory Space, and IO Space */
  103. pci_hose_read_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, &tmp32);
  104. pci_hose_write_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, 0x08);
  105. pci_hose_read_config_dword(hose, dev_nu, PCI_CACHE_LINE_SIZE, &tmp32);
  106. pci_hose_read_config_dword(hose, dev_nu, PCI_COMMAND, &tmp32);
  107. tmp32 &= 0xffff;
  108. if (iovsmem == 0)
  109. tmp32 |= 0x5;
  110. else
  111. tmp32 |= 0x6;
  112. pci_hose_write_config_dword(hose, dev_nu, PCI_COMMAND, tmp32);
  113. }
  114. static void pci_bus_scan(struct ftpci100_data *priv)
  115. {
  116. struct pci_controller *hose = (struct pci_controller *)&local_hose;
  117. unsigned int bus, dev, func;
  118. pci_dev_t dev_nu;
  119. unsigned int data32;
  120. unsigned int tmp;
  121. unsigned char header;
  122. unsigned char int_pin;
  123. unsigned int niobars;
  124. unsigned int nmbars;
  125. priv->ndevs = 1;
  126. nmbars = 0;
  127. niobars = 0;
  128. for (bus = 0; bus < MAX_BUS_NUM; bus++)
  129. for (dev = 0; dev < MAX_DEV_NUM; dev++)
  130. for (func = 0; func < MAX_FUN_NUM; func++) {
  131. dev_nu = PCI_BDF(bus, dev, func);
  132. pci_hose_read_config_dword(hose, dev_nu,
  133. PCI_VENDOR_ID, &data32);
  134. /*
  135. * some broken boards return 0 or ~0,
  136. * if a slot is empty.
  137. */
  138. if (data32 == 0xffffffff ||
  139. data32 == 0x00000000 ||
  140. data32 == 0x0000ffff ||
  141. data32 == 0xffff0000)
  142. continue;
  143. pci_hose_read_config_dword(hose, dev_nu,
  144. PCI_HEADER_TYPE, &tmp);
  145. header = (unsigned char)tmp;
  146. setup_pci_bar(bus, dev, func, header, priv);
  147. devs[priv->ndevs].v_id = (u16)(data32 & \
  148. 0x0000ffff);
  149. devs[priv->ndevs].d_id = (u16)((data32 & \
  150. 0xffff0000) >> 16);
  151. /* Figure out what INTX# line the card uses */
  152. pci_hose_read_config_byte(hose, dev_nu,
  153. PCI_INTERRUPT_PIN, &int_pin);
  154. /* assign the appropriate irq line */
  155. if (int_pin > PCI_IRQ_LINES) {
  156. printf("more irq lines than expect\n");
  157. } else if (int_pin != 0) {
  158. /* This device uses an interrupt line */
  159. devs[priv->ndevs].pin = int_pin;
  160. }
  161. pci_hose_read_config_dword(hose, dev_nu,
  162. PCI_CLASS_DEVICE, &data32);
  163. debug("%06d %03d %03d " \
  164. "%04d %08x %08x " \
  165. "%03d %08x %06d %08x\n",
  166. priv->ndevs, devs[priv->ndevs].bus,
  167. devs[priv->ndevs].dev,
  168. devs[priv->ndevs].func,
  169. devs[priv->ndevs].d_id,
  170. devs[priv->ndevs].v_id,
  171. devs[priv->ndevs].pin,
  172. devs[priv->ndevs].bar[0].addr,
  173. devs[priv->ndevs].bar[0].size,
  174. data32 >> 8);
  175. priv->ndevs++;
  176. }
  177. }
  178. static void ftpci_preinit(struct ftpci100_data *priv)
  179. {
  180. struct ftpci100_ahbc *ftpci100;
  181. struct pci_controller *hose = (struct pci_controller *)&local_hose;
  182. u32 pci_config_addr;
  183. u32 pci_config_data;
  184. priv->reg_base = CONFIG_FTPCI100_BASE;
  185. priv->io_base = CONFIG_FTPCI100_BASE + CONFIG_FTPCI100_IO_SIZE;
  186. priv->mmio_base = CONFIG_FTPCI100_MEM_BASE;
  187. priv->mem_base = CONFIG_FTPCI100_MEM_BASE + CONFIG_FTPCI100_MEM_SIZE;
  188. ftpci100 = (struct ftpci100_ahbc *)priv->reg_base;
  189. pci_config_addr = (u32) &ftpci100->conf;
  190. pci_config_data = (u32) &ftpci100->data;
  191. /* print device name */
  192. printf("FTPCI100\n");
  193. /* dump basic configuration */
  194. debug("%s: Config addr is %08X, data port is %08X\n",
  195. __func__, pci_config_addr, pci_config_data);
  196. /* PCI memory space */
  197. pci_set_region(hose->regions + 0,
  198. CONFIG_PCI_MEM_BUS,
  199. CONFIG_PCI_MEM_PHYS,
  200. CONFIG_PCI_MEM_SIZE,
  201. PCI_REGION_MEM);
  202. hose->region_count++;
  203. /* PCI IO space */
  204. pci_set_region(hose->regions + 1,
  205. CONFIG_PCI_IO_BUS,
  206. CONFIG_PCI_IO_PHYS,
  207. CONFIG_PCI_IO_SIZE,
  208. PCI_REGION_IO);
  209. hose->region_count++;
  210. #if defined(CONFIG_PCI_SYS_BUS)
  211. /* PCI System Memory space */
  212. pci_set_region(hose->regions + 2,
  213. CONFIG_PCI_SYS_BUS,
  214. CONFIG_PCI_SYS_PHYS,
  215. CONFIG_PCI_SYS_SIZE,
  216. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  217. hose->region_count++;
  218. #endif
  219. /* setup indirect read/write function */
  220. pci_setup_indirect(hose, pci_config_addr, pci_config_data);
  221. /* register hose */
  222. pci_register_hose(hose);
  223. }
  224. void pci_ftpci_init(void)
  225. {
  226. struct ftpci100_data *priv = NULL;
  227. struct pci_controller *hose = (struct pci_controller *)&local_hose;
  228. pci_dev_t bridge_num;
  229. struct pci_device_id bridge_ids[] = {
  230. {FTPCI100_BRIDGE_VENDORID, FTPCI100_BRIDGE_DEVICEID},
  231. {0, 0}
  232. };
  233. priv = malloc(sizeof(struct ftpci100_data));
  234. if (!priv) {
  235. printf("%s(): failed to malloc priv\n", __func__);
  236. return;
  237. }
  238. memset(priv, 0, sizeof(struct ftpci100_data));
  239. ftpci_preinit(priv);
  240. debug("Device bus dev func deviceID vendorID pin address" \
  241. " size class\n");
  242. pci_bus_scan(priv);
  243. /*
  244. * Setup the PCI Bridge Window to 1GB,
  245. * it will cause USB OHCI Host controller Unrecoverable Error
  246. * if it is not set.
  247. */
  248. bridge_num = pci_find_devices(bridge_ids, 0);
  249. if (bridge_num == -1) {
  250. printf("PCI Bridge not found\n");
  251. return;
  252. }
  253. pci_hose_write_config_dword(hose, bridge_num, PCI_MEM_BASE_SIZE1,
  254. FTPCI100_BASE_ADR_SIZE(1024));
  255. }