pci_ftpci100.c 8.6 KB

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