pci.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (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,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /* PCI.c - PCI functions */
  25. #include <common.h>
  26. #ifdef CONFIG_PCI
  27. #include <pci.h>
  28. #ifdef CONFIG_PCI_PNP
  29. void pciauto_config_init(struct pci_controller *hose);
  30. int pciauto_region_allocate(struct pci_region* res, unsigned int size, unsigned int *bar);
  31. #endif
  32. #include "../../Marvell/include/pci.h"
  33. #undef DEBUG
  34. #undef IDE_SET_NATIVE_MODE
  35. static unsigned int local_buses[] = { 0, 0 };
  36. static const unsigned char pci_irq_swizzle[2][PCI_MAX_DEVICES] = {
  37. {0, 0, 0, 0, 0, 0, 0, 27, 27, [9 ... PCI_MAX_DEVICES - 1] = 0 },
  38. {0, 0, 0, 0, 0, 0, 0, 29, 29, [9 ... PCI_MAX_DEVICES - 1] = 0 },
  39. };
  40. #ifdef CONFIG_USE_CPCIDVI
  41. typedef struct {
  42. unsigned int base;
  43. unsigned int init;
  44. } GT_CPCIDVI_ROM_T;
  45. static GT_CPCIDVI_ROM_T gt_cpcidvi_rom = {0, 0};
  46. #endif
  47. #ifdef DEBUG
  48. static const unsigned int pci_bus_list[] = { PCI_0_MODE, PCI_1_MODE };
  49. static void gt_pci_bus_mode_display (PCI_HOST host)
  50. {
  51. unsigned int mode;
  52. mode = (GTREGREAD (pci_bus_list[host]) & (BIT4 | BIT5)) >> 4;
  53. switch (mode) {
  54. case 0:
  55. printf ("PCI %d bus mode: Conventional PCI\n", host);
  56. break;
  57. case 1:
  58. printf ("PCI %d bus mode: 66 MHz PCIX\n", host);
  59. break;
  60. case 2:
  61. printf ("PCI %d bus mode: 100 MHz PCIX\n", host);
  62. break;
  63. case 3:
  64. printf ("PCI %d bus mode: 133 MHz PCIX\n", host);
  65. break;
  66. default:
  67. printf ("Unknown BUS %d\n", mode);
  68. }
  69. }
  70. #endif
  71. static const unsigned int pci_p2p_configuration_reg[] = {
  72. PCI_0P2P_CONFIGURATION, PCI_1P2P_CONFIGURATION
  73. };
  74. static const unsigned int pci_configuration_address[] = {
  75. PCI_0CONFIGURATION_ADDRESS, PCI_1CONFIGURATION_ADDRESS
  76. };
  77. static const unsigned int pci_configuration_data[] = {
  78. PCI_0CONFIGURATION_DATA_VIRTUAL_REGISTER,
  79. PCI_1CONFIGURATION_DATA_VIRTUAL_REGISTER
  80. };
  81. static const unsigned int pci_error_cause_reg[] = {
  82. PCI_0ERROR_CAUSE, PCI_1ERROR_CAUSE
  83. };
  84. static const unsigned int pci_arbiter_control[] = {
  85. PCI_0ARBITER_CONTROL, PCI_1ARBITER_CONTROL
  86. };
  87. static const unsigned int pci_address_space_en[] = {
  88. PCI_0_BASE_ADDR_REG_ENABLE, PCI_1_BASE_ADDR_REG_ENABLE
  89. };
  90. static const unsigned int pci_snoop_control_base_0_low[] = {
  91. PCI_0SNOOP_CONTROL_BASE_0_LOW, PCI_1SNOOP_CONTROL_BASE_0_LOW
  92. };
  93. static const unsigned int pci_snoop_control_top_0[] = {
  94. PCI_0SNOOP_CONTROL_TOP_0, PCI_1SNOOP_CONTROL_TOP_0
  95. };
  96. static const unsigned int pci_access_control_base_0_low[] = {
  97. PCI_0ACCESS_CONTROL_BASE_0_LOW, PCI_1ACCESS_CONTROL_BASE_0_LOW
  98. };
  99. static const unsigned int pci_access_control_top_0[] = {
  100. PCI_0ACCESS_CONTROL_TOP_0, PCI_1ACCESS_CONTROL_TOP_0
  101. };
  102. static const unsigned int pci_scs_bank_size[2][4] = {
  103. {PCI_0SCS_0_BANK_SIZE, PCI_0SCS_1_BANK_SIZE,
  104. PCI_0SCS_2_BANK_SIZE, PCI_0SCS_3_BANK_SIZE},
  105. {PCI_1SCS_0_BANK_SIZE, PCI_1SCS_1_BANK_SIZE,
  106. PCI_1SCS_2_BANK_SIZE, PCI_1SCS_3_BANK_SIZE}
  107. };
  108. static const unsigned int pci_p2p_configuration[] = {
  109. PCI_0P2P_CONFIGURATION, PCI_1P2P_CONFIGURATION
  110. };
  111. /********************************************************************
  112. * pciWriteConfigReg - Write to a PCI configuration register
  113. * - Make sure the GT is configured as a master before writing
  114. * to another device on the PCI.
  115. * - The function takes care of Big/Little endian conversion.
  116. *
  117. *
  118. * Inputs: unsigned int regOffset: The register offset as it apears in the GT spec
  119. * (or any other PCI device spec)
  120. * pciDevNum: The device number needs to be addressed.
  121. *
  122. * Configuration Address 0xCF8:
  123. *
  124. * 31 30 24 23 16 15 11 10 8 7 2 0 <=bit Number
  125. * |congif|Reserved| Bus |Device|Function|Register|00|
  126. * |Enable| |Number|Number| Number | Number | | <=field Name
  127. *
  128. *********************************************************************/
  129. void pciWriteConfigReg (PCI_HOST host, unsigned int regOffset,
  130. unsigned int pciDevNum, unsigned int data)
  131. {
  132. volatile unsigned int DataForAddrReg;
  133. unsigned int functionNum;
  134. unsigned int busNum = 0;
  135. unsigned int addr;
  136. if (pciDevNum > 32) /* illegal device Number */
  137. return;
  138. if (pciDevNum == SELF) { /* configure our configuration space. */
  139. pciDevNum =
  140. (GTREGREAD (pci_p2p_configuration_reg[host]) >> 24) &
  141. 0x1f;
  142. busNum = GTREGREAD (pci_p2p_configuration_reg[host]) &
  143. 0xff0000;
  144. }
  145. functionNum = regOffset & 0x00000700;
  146. pciDevNum = pciDevNum << 11;
  147. regOffset = regOffset & 0xfc;
  148. DataForAddrReg =
  149. (regOffset | pciDevNum | functionNum | busNum) | BIT31;
  150. GT_REG_WRITE (pci_configuration_address[host], DataForAddrReg);
  151. GT_REG_READ (pci_configuration_address[host], &addr);
  152. if (addr != DataForAddrReg)
  153. return;
  154. GT_REG_WRITE (pci_configuration_data[host], data);
  155. }
  156. /********************************************************************
  157. * pciReadConfigReg - Read from a PCI0 configuration register
  158. * - Make sure the GT is configured as a master before reading
  159. * from another device on the PCI.
  160. * - The function takes care of Big/Little endian conversion.
  161. * INPUTS: regOffset: The register offset as it apears in the GT spec (or PCI
  162. * spec)
  163. * pciDevNum: The device number needs to be addressed.
  164. * RETURNS: data , if the data == 0xffffffff check the master abort bit in the
  165. * cause register to make sure the data is valid
  166. *
  167. * Configuration Address 0xCF8:
  168. *
  169. * 31 30 24 23 16 15 11 10 8 7 2 0 <=bit Number
  170. * |congif|Reserved| Bus |Device|Function|Register|00|
  171. * |Enable| |Number|Number| Number | Number | | <=field Name
  172. *
  173. *********************************************************************/
  174. unsigned int pciReadConfigReg (PCI_HOST host, unsigned int regOffset,
  175. unsigned int pciDevNum)
  176. {
  177. volatile unsigned int DataForAddrReg;
  178. unsigned int data;
  179. unsigned int functionNum;
  180. unsigned int busNum = 0;
  181. if (pciDevNum > 32) /* illegal device Number */
  182. return 0xffffffff;
  183. if (pciDevNum == SELF) { /* configure our configuration space. */
  184. pciDevNum =
  185. (GTREGREAD (pci_p2p_configuration_reg[host]) >> 24) &
  186. 0x1f;
  187. busNum = GTREGREAD (pci_p2p_configuration_reg[host]) &
  188. 0xff0000;
  189. }
  190. functionNum = regOffset & 0x00000700;
  191. pciDevNum = pciDevNum << 11;
  192. regOffset = regOffset & 0xfc;
  193. DataForAddrReg =
  194. (regOffset | pciDevNum | functionNum | busNum) | BIT31;
  195. GT_REG_WRITE (pci_configuration_address[host], DataForAddrReg);
  196. GT_REG_READ (pci_configuration_address[host], &data);
  197. if (data != DataForAddrReg)
  198. return 0xffffffff;
  199. GT_REG_READ (pci_configuration_data[host], &data);
  200. return data;
  201. }
  202. /********************************************************************
  203. * pciOverBridgeWriteConfigReg - Write to a PCI configuration register where
  204. * the agent is placed on another Bus. For more
  205. * information read P2P in the PCI spec.
  206. *
  207. * Inputs: unsigned int regOffset - The register offset as it apears in the
  208. * GT spec (or any other PCI device spec).
  209. * unsigned int pciDevNum - The device number needs to be addressed.
  210. * unsigned int busNum - On which bus does the Target agent connect
  211. * to.
  212. * unsigned int data - data to be written.
  213. *
  214. * Configuration Address 0xCF8:
  215. *
  216. * 31 30 24 23 16 15 11 10 8 7 2 0 <=bit Number
  217. * |congif|Reserved| Bus |Device|Function|Register|01|
  218. * |Enable| |Number|Number| Number | Number | | <=field Name
  219. *
  220. * The configuration Address is configure as type-I (bits[1:0] = '01') due to
  221. * PCI spec referring to P2P.
  222. *
  223. *********************************************************************/
  224. void pciOverBridgeWriteConfigReg (PCI_HOST host,
  225. unsigned int regOffset,
  226. unsigned int pciDevNum,
  227. unsigned int busNum, unsigned int data)
  228. {
  229. unsigned int DataForReg;
  230. unsigned int functionNum;
  231. functionNum = regOffset & 0x00000700;
  232. pciDevNum = pciDevNum << 11;
  233. regOffset = regOffset & 0xff;
  234. busNum = busNum << 16;
  235. if (pciDevNum == SELF) { /* This board */
  236. DataForReg = (regOffset | pciDevNum | functionNum) | BIT0;
  237. } else {
  238. DataForReg = (regOffset | pciDevNum | functionNum | busNum) |
  239. BIT31 | BIT0;
  240. }
  241. GT_REG_WRITE (pci_configuration_address[host], DataForReg);
  242. GT_REG_WRITE (pci_configuration_data[host], data);
  243. }
  244. /********************************************************************
  245. * pciOverBridgeReadConfigReg - Read from a PCIn configuration register where
  246. * the agent target locate on another PCI bus.
  247. * - Make sure the GT is configured as a master
  248. * before reading from another device on the PCI.
  249. * - The function takes care of Big/Little endian
  250. * conversion.
  251. * INPUTS: regOffset: The register offset as it apears in the GT spec (or PCI
  252. * spec). (configuration register offset.)
  253. * pciDevNum: The device number needs to be addressed.
  254. * busNum: the Bus number where the agent is place.
  255. * RETURNS: data , if the data == 0xffffffff check the master abort bit in the
  256. * cause register to make sure the data is valid
  257. *
  258. * Configuration Address 0xCF8:
  259. *
  260. * 31 30 24 23 16 15 11 10 8 7 2 0 <=bit Number
  261. * |congif|Reserved| Bus |Device|Function|Register|01|
  262. * |Enable| |Number|Number| Number | Number | | <=field Name
  263. *
  264. *********************************************************************/
  265. unsigned int pciOverBridgeReadConfigReg (PCI_HOST host,
  266. unsigned int regOffset,
  267. unsigned int pciDevNum,
  268. unsigned int busNum)
  269. {
  270. unsigned int DataForReg;
  271. unsigned int data;
  272. unsigned int functionNum;
  273. functionNum = regOffset & 0x00000700;
  274. pciDevNum = pciDevNum << 11;
  275. regOffset = regOffset & 0xff;
  276. busNum = busNum << 16;
  277. if (pciDevNum == SELF) { /* This board */
  278. DataForReg = (regOffset | pciDevNum | functionNum) | BIT31;
  279. } else { /* agent on another bus */
  280. DataForReg = (regOffset | pciDevNum | functionNum | busNum) |
  281. BIT0 | BIT31;
  282. }
  283. GT_REG_WRITE (pci_configuration_address[host], DataForReg);
  284. GT_REG_READ (pci_configuration_data[host], &data);
  285. return data;
  286. }
  287. /********************************************************************
  288. * pciGetRegOffset - Gets the register offset for this region config.
  289. *
  290. * INPUT: Bus, Region - The bus and region we ask for its base address.
  291. * OUTPUT: N/A
  292. * RETURNS: PCI register base address
  293. *********************************************************************/
  294. static unsigned int pciGetRegOffset (PCI_HOST host, PCI_REGION region)
  295. {
  296. switch (host) {
  297. case PCI_HOST0:
  298. switch (region) {
  299. case PCI_IO:
  300. return PCI_0I_O_LOW_DECODE_ADDRESS;
  301. case PCI_REGION0:
  302. return PCI_0MEMORY0_LOW_DECODE_ADDRESS;
  303. case PCI_REGION1:
  304. return PCI_0MEMORY1_LOW_DECODE_ADDRESS;
  305. case PCI_REGION2:
  306. return PCI_0MEMORY2_LOW_DECODE_ADDRESS;
  307. case PCI_REGION3:
  308. return PCI_0MEMORY3_LOW_DECODE_ADDRESS;
  309. }
  310. case PCI_HOST1:
  311. switch (region) {
  312. case PCI_IO:
  313. return PCI_1I_O_LOW_DECODE_ADDRESS;
  314. case PCI_REGION0:
  315. return PCI_1MEMORY0_LOW_DECODE_ADDRESS;
  316. case PCI_REGION1:
  317. return PCI_1MEMORY1_LOW_DECODE_ADDRESS;
  318. case PCI_REGION2:
  319. return PCI_1MEMORY2_LOW_DECODE_ADDRESS;
  320. case PCI_REGION3:
  321. return PCI_1MEMORY3_LOW_DECODE_ADDRESS;
  322. }
  323. }
  324. return PCI_0MEMORY0_LOW_DECODE_ADDRESS;
  325. }
  326. static unsigned int pciGetRemapOffset (PCI_HOST host, PCI_REGION region)
  327. {
  328. switch (host) {
  329. case PCI_HOST0:
  330. switch (region) {
  331. case PCI_IO:
  332. return PCI_0I_O_ADDRESS_REMAP;
  333. case PCI_REGION0:
  334. return PCI_0MEMORY0_ADDRESS_REMAP;
  335. case PCI_REGION1:
  336. return PCI_0MEMORY1_ADDRESS_REMAP;
  337. case PCI_REGION2:
  338. return PCI_0MEMORY2_ADDRESS_REMAP;
  339. case PCI_REGION3:
  340. return PCI_0MEMORY3_ADDRESS_REMAP;
  341. }
  342. case PCI_HOST1:
  343. switch (region) {
  344. case PCI_IO:
  345. return PCI_1I_O_ADDRESS_REMAP;
  346. case PCI_REGION0:
  347. return PCI_1MEMORY0_ADDRESS_REMAP;
  348. case PCI_REGION1:
  349. return PCI_1MEMORY1_ADDRESS_REMAP;
  350. case PCI_REGION2:
  351. return PCI_1MEMORY2_ADDRESS_REMAP;
  352. case PCI_REGION3:
  353. return PCI_1MEMORY3_ADDRESS_REMAP;
  354. }
  355. }
  356. return PCI_0MEMORY0_ADDRESS_REMAP;
  357. }
  358. /********************************************************************
  359. * pciGetBaseAddress - Gets the base address of a PCI.
  360. * - If the PCI size is 0 then this base address has no meaning!!!
  361. *
  362. *
  363. * INPUT: Bus, Region - The bus and region we ask for its base address.
  364. * OUTPUT: N/A
  365. * RETURNS: PCI base address.
  366. *********************************************************************/
  367. unsigned int pciGetBaseAddress (PCI_HOST host, PCI_REGION region)
  368. {
  369. unsigned int regBase;
  370. unsigned int regEnd;
  371. unsigned int regOffset = pciGetRegOffset (host, region);
  372. GT_REG_READ (regOffset, &regBase);
  373. GT_REG_READ (regOffset + 8, &regEnd);
  374. if (regEnd <= regBase)
  375. return 0xffffffff; /* ERROR !!! */
  376. regBase = regBase << 16;
  377. return regBase;
  378. }
  379. bool pciMapSpace (PCI_HOST host, PCI_REGION region, unsigned int remapBase,
  380. unsigned int bankBase, unsigned int bankLength)
  381. {
  382. unsigned int low = 0xfff;
  383. unsigned int high = 0x0;
  384. unsigned int regOffset = pciGetRegOffset (host, region);
  385. unsigned int remapOffset = pciGetRemapOffset (host, region);
  386. if (bankLength != 0) {
  387. low = (bankBase >> 16) & 0xffff;
  388. high = ((bankBase + bankLength) >> 16) - 1;
  389. }
  390. GT_REG_WRITE (regOffset, low | (1 << 24)); /* no swapping */
  391. GT_REG_WRITE (regOffset + 8, high);
  392. if (bankLength != 0) { /* must do AFTER writing maps */
  393. GT_REG_WRITE (remapOffset, remapBase >> 16); /* sorry, 32 bits only.
  394. dont support upper 32
  395. in this driver */
  396. }
  397. return true;
  398. }
  399. unsigned int pciGetSpaceBase (PCI_HOST host, PCI_REGION region)
  400. {
  401. unsigned int low;
  402. unsigned int regOffset = pciGetRegOffset (host, region);
  403. GT_REG_READ (regOffset, &low);
  404. return (low & 0xffff) << 16;
  405. }
  406. unsigned int pciGetSpaceSize (PCI_HOST host, PCI_REGION region)
  407. {
  408. unsigned int low, high;
  409. unsigned int regOffset = pciGetRegOffset (host, region);
  410. GT_REG_READ (regOffset, &low);
  411. GT_REG_READ (regOffset + 8, &high);
  412. return ((high & 0xffff) + 1) << 16;
  413. }
  414. /* ronen - 7/Dec/03*/
  415. /********************************************************************
  416. * gtPciDisable/EnableInternalBAR - This function enable/disable PCI BARS.
  417. * Inputs: one of the PCI BAR
  418. *********************************************************************/
  419. void gtPciEnableInternalBAR (PCI_HOST host, PCI_INTERNAL_BAR pciBAR)
  420. {
  421. RESET_REG_BITS (pci_address_space_en[host], BIT0 << pciBAR);
  422. }
  423. void gtPciDisableInternalBAR (PCI_HOST host, PCI_INTERNAL_BAR pciBAR)
  424. {
  425. SET_REG_BITS (pci_address_space_en[host], BIT0 << pciBAR);
  426. }
  427. /********************************************************************
  428. * pciMapMemoryBank - Maps PCI_host memory bank "bank" for the slave.
  429. *
  430. * Inputs: base and size of PCI SCS
  431. *********************************************************************/
  432. void pciMapMemoryBank (PCI_HOST host, MEMORY_BANK bank,
  433. unsigned int pciDramBase, unsigned int pciDramSize)
  434. {
  435. /*ronen different function for 3rd bank. */
  436. unsigned int offset = (bank < 2) ? bank * 8 : 0x100 + (bank - 2) * 8;
  437. pciDramBase = pciDramBase & 0xfffff000;
  438. pciDramBase = pciDramBase | (pciReadConfigReg (host,
  439. PCI_SCS_0_BASE_ADDRESS
  440. + offset,
  441. SELF) & 0x00000fff);
  442. pciWriteConfigReg (host, PCI_SCS_0_BASE_ADDRESS + offset, SELF,
  443. pciDramBase);
  444. if (pciDramSize == 0)
  445. pciDramSize++;
  446. GT_REG_WRITE (pci_scs_bank_size[host][bank], pciDramSize - 1);
  447. gtPciEnableInternalBAR (host, bank);
  448. }
  449. /********************************************************************
  450. * pciSetRegionFeatures - This function modifys one of the 8 regions with
  451. * feature bits given as an input.
  452. * - Be advised to check the spec before modifying them.
  453. * Inputs: PCI_PROTECT_REGION region - one of the eight regions.
  454. * unsigned int features - See file: pci.h there are defintion for those
  455. * region features.
  456. * unsigned int baseAddress - The region base Address.
  457. * unsigned int topAddress - The region top Address.
  458. * Returns: false if one of the parameters is erroneous true otherwise.
  459. *********************************************************************/
  460. bool pciSetRegionFeatures (PCI_HOST host, PCI_ACCESS_REGIONS region,
  461. unsigned int features, unsigned int baseAddress,
  462. unsigned int regionLength)
  463. {
  464. unsigned int accessLow;
  465. unsigned int accessHigh;
  466. unsigned int accessTop = baseAddress + regionLength;
  467. if (regionLength == 0) { /* close the region. */
  468. pciDisableAccessRegion (host, region);
  469. return true;
  470. }
  471. /* base Address is store is bits [11:0] */
  472. accessLow = (baseAddress & 0xfff00000) >> 20;
  473. /* All the features are update according to the defines in pci.h (to be on
  474. the safe side we disable bits: [11:0] */
  475. accessLow = accessLow | (features & 0xfffff000);
  476. /* write to the Low Access Region register */
  477. GT_REG_WRITE (pci_access_control_base_0_low[host] + 0x10 * region,
  478. accessLow);
  479. accessHigh = (accessTop & 0xfff00000) >> 20;
  480. /* write to the High Access Region register */
  481. GT_REG_WRITE (pci_access_control_top_0[host] + 0x10 * region,
  482. accessHigh - 1);
  483. return true;
  484. }
  485. /********************************************************************
  486. * pciDisableAccessRegion - Disable The given Region by writing MAX size
  487. * to its low Address and MIN size to its high Address.
  488. *
  489. * Inputs: PCI_ACCESS_REGIONS region - The region we to be Disabled.
  490. * Returns: N/A.
  491. *********************************************************************/
  492. void pciDisableAccessRegion (PCI_HOST host, PCI_ACCESS_REGIONS region)
  493. {
  494. /* writing back the registers default values. */
  495. GT_REG_WRITE (pci_access_control_base_0_low[host] + 0x10 * region,
  496. 0x01001fff);
  497. GT_REG_WRITE (pci_access_control_top_0[host] + 0x10 * region, 0);
  498. }
  499. /********************************************************************
  500. * pciArbiterEnable - Enables PCI-0`s Arbitration mechanism.
  501. *
  502. * Inputs: N/A
  503. * Returns: true.
  504. *********************************************************************/
  505. bool pciArbiterEnable (PCI_HOST host)
  506. {
  507. unsigned int regData;
  508. GT_REG_READ (pci_arbiter_control[host], &regData);
  509. GT_REG_WRITE (pci_arbiter_control[host], regData | BIT31);
  510. return true;
  511. }
  512. /********************************************************************
  513. * pciArbiterDisable - Disable PCI-0`s Arbitration mechanism.
  514. *
  515. * Inputs: N/A
  516. * Returns: true
  517. *********************************************************************/
  518. bool pciArbiterDisable (PCI_HOST host)
  519. {
  520. unsigned int regData;
  521. GT_REG_READ (pci_arbiter_control[host], &regData);
  522. GT_REG_WRITE (pci_arbiter_control[host], regData & 0x7fffffff);
  523. return true;
  524. }
  525. /********************************************************************
  526. * pciSetArbiterAgentsPriority - Priority setup for the PCI agents (Hi or Low)
  527. *
  528. * Inputs: PCI_AGENT_PRIO internalAgent - priotity for internal agent.
  529. * PCI_AGENT_PRIO externalAgent0 - priotity for external#0 agent.
  530. * PCI_AGENT_PRIO externalAgent1 - priotity for external#1 agent.
  531. * PCI_AGENT_PRIO externalAgent2 - priotity for external#2 agent.
  532. * PCI_AGENT_PRIO externalAgent3 - priotity for external#3 agent.
  533. * PCI_AGENT_PRIO externalAgent4 - priotity for external#4 agent.
  534. * PCI_AGENT_PRIO externalAgent5 - priotity for external#5 agent.
  535. * Returns: true
  536. *********************************************************************/
  537. bool pciSetArbiterAgentsPriority (PCI_HOST host, PCI_AGENT_PRIO internalAgent,
  538. PCI_AGENT_PRIO externalAgent0,
  539. PCI_AGENT_PRIO externalAgent1,
  540. PCI_AGENT_PRIO externalAgent2,
  541. PCI_AGENT_PRIO externalAgent3,
  542. PCI_AGENT_PRIO externalAgent4,
  543. PCI_AGENT_PRIO externalAgent5)
  544. {
  545. unsigned int regData;
  546. unsigned int writeData;
  547. GT_REG_READ (pci_arbiter_control[host], &regData);
  548. writeData = (internalAgent << 7) + (externalAgent0 << 8) +
  549. (externalAgent1 << 9) + (externalAgent2 << 10) +
  550. (externalAgent3 << 11) + (externalAgent4 << 12) +
  551. (externalAgent5 << 13);
  552. regData = (regData & 0xffffc07f) | writeData;
  553. GT_REG_WRITE (pci_arbiter_control[host], regData & regData);
  554. return true;
  555. }
  556. /********************************************************************
  557. * pciParkingDisable - Park on last option disable, with this function you can
  558. * disable the park on last mechanism for each agent.
  559. * disabling this option for all agents results parking
  560. * on the internal master.
  561. *
  562. * Inputs: PCI_AGENT_PARK internalAgent - parking Disable for internal agent.
  563. * PCI_AGENT_PARK externalAgent0 - parking Disable for external#0 agent.
  564. * PCI_AGENT_PARK externalAgent1 - parking Disable for external#1 agent.
  565. * PCI_AGENT_PARK externalAgent2 - parking Disable for external#2 agent.
  566. * PCI_AGENT_PARK externalAgent3 - parking Disable for external#3 agent.
  567. * PCI_AGENT_PARK externalAgent4 - parking Disable for external#4 agent.
  568. * PCI_AGENT_PARK externalAgent5 - parking Disable for external#5 agent.
  569. * Returns: true
  570. *********************************************************************/
  571. bool pciParkingDisable (PCI_HOST host, PCI_AGENT_PARK internalAgent,
  572. PCI_AGENT_PARK externalAgent0,
  573. PCI_AGENT_PARK externalAgent1,
  574. PCI_AGENT_PARK externalAgent2,
  575. PCI_AGENT_PARK externalAgent3,
  576. PCI_AGENT_PARK externalAgent4,
  577. PCI_AGENT_PARK externalAgent5)
  578. {
  579. unsigned int regData;
  580. unsigned int writeData;
  581. GT_REG_READ (pci_arbiter_control[host], &regData);
  582. writeData = (internalAgent << 14) + (externalAgent0 << 15) +
  583. (externalAgent1 << 16) + (externalAgent2 << 17) +
  584. (externalAgent3 << 18) + (externalAgent4 << 19) +
  585. (externalAgent5 << 20);
  586. regData = (regData & ~(0x7f << 14)) | writeData;
  587. GT_REG_WRITE (pci_arbiter_control[host], regData);
  588. return true;
  589. }
  590. /********************************************************************
  591. * pciEnableBrokenAgentDetection - A master is said to be broken if it fails to
  592. * respond to grant assertion within a window specified in
  593. * the input value: 'brokenValue'.
  594. *
  595. * Inputs: unsigned char brokenValue - A value which limits the Master to hold the
  596. * grant without asserting frame.
  597. * Returns: Error for illegal broken value otherwise true.
  598. *********************************************************************/
  599. bool pciEnableBrokenAgentDetection (PCI_HOST host, unsigned char brokenValue)
  600. {
  601. unsigned int data;
  602. unsigned int regData;
  603. if (brokenValue > 0xf)
  604. return false; /* brokenValue must be 4 bit */
  605. data = brokenValue << 3;
  606. GT_REG_READ (pci_arbiter_control[host], &regData);
  607. regData = (regData & 0xffffff87) | data;
  608. GT_REG_WRITE (pci_arbiter_control[host], regData | BIT1);
  609. return true;
  610. }
  611. /********************************************************************
  612. * pciDisableBrokenAgentDetection - This function disable the Broken agent
  613. * Detection mechanism.
  614. * NOTE: This operation may cause a dead lock on the
  615. * pci0 arbitration.
  616. *
  617. * Inputs: N/A
  618. * Returns: true.
  619. *********************************************************************/
  620. bool pciDisableBrokenAgentDetection (PCI_HOST host)
  621. {
  622. unsigned int regData;
  623. GT_REG_READ (pci_arbiter_control[host], &regData);
  624. regData = regData & 0xfffffffd;
  625. GT_REG_WRITE (pci_arbiter_control[host], regData);
  626. return true;
  627. }
  628. /********************************************************************
  629. * pciP2PConfig - This function set the PCI_n P2P configurate.
  630. * For more information on the P2P read PCI spec.
  631. *
  632. * Inputs: unsigned int SecondBusLow - Secondery PCI interface Bus Range Lower
  633. * Boundry.
  634. * unsigned int SecondBusHigh - Secondry PCI interface Bus Range upper
  635. * Boundry.
  636. * unsigned int busNum - The CPI bus number to which the PCI interface
  637. * is connected.
  638. * unsigned int devNum - The PCI interface's device number.
  639. *
  640. * Returns: true.
  641. *********************************************************************/
  642. bool pciP2PConfig (PCI_HOST host, unsigned int SecondBusLow,
  643. unsigned int SecondBusHigh,
  644. unsigned int busNum, unsigned int devNum)
  645. {
  646. unsigned int regData;
  647. regData = (SecondBusLow & 0xff) | ((SecondBusHigh & 0xff) << 8) |
  648. ((busNum & 0xff) << 16) | ((devNum & 0x1f) << 24);
  649. GT_REG_WRITE (pci_p2p_configuration[host], regData);
  650. return true;
  651. }
  652. /********************************************************************
  653. * pciSetRegionSnoopMode - This function modifys one of the 4 regions which
  654. * supports Cache Coherency in the PCI_n interface.
  655. * Inputs: region - One of the four regions.
  656. * snoopType - There is four optional Types:
  657. * 1. No Snoop.
  658. * 2. Snoop to WT region.
  659. * 3. Snoop to WB region.
  660. * 4. Snoop & Invalidate to WB region.
  661. * baseAddress - Base Address of this region.
  662. * regionLength - Region length.
  663. * Returns: false if one of the parameters is wrong otherwise return true.
  664. *********************************************************************/
  665. bool pciSetRegionSnoopMode (PCI_HOST host, PCI_SNOOP_REGION region,
  666. PCI_SNOOP_TYPE snoopType,
  667. unsigned int baseAddress,
  668. unsigned int regionLength)
  669. {
  670. unsigned int snoopXbaseAddress;
  671. unsigned int snoopXtopAddress;
  672. unsigned int data;
  673. unsigned int snoopHigh = baseAddress + regionLength;
  674. if ((region > PCI_SNOOP_REGION3) || (snoopType > PCI_SNOOP_WB))
  675. return false;
  676. snoopXbaseAddress =
  677. pci_snoop_control_base_0_low[host] + 0x10 * region;
  678. snoopXtopAddress = pci_snoop_control_top_0[host] + 0x10 * region;
  679. if (regionLength == 0) { /* closing the region */
  680. GT_REG_WRITE (snoopXbaseAddress, 0x0000ffff);
  681. GT_REG_WRITE (snoopXtopAddress, 0);
  682. return true;
  683. }
  684. baseAddress = baseAddress & 0xfff00000; /* Granularity of 1MByte */
  685. data = (baseAddress >> 20) | snoopType << 12;
  686. GT_REG_WRITE (snoopXbaseAddress, data);
  687. snoopHigh = (snoopHigh & 0xfff00000) >> 20;
  688. GT_REG_WRITE (snoopXtopAddress, snoopHigh - 1);
  689. return true;
  690. }
  691. static int gt_read_config_dword (struct pci_controller *hose,
  692. pci_dev_t dev, int offset, u32 * value)
  693. {
  694. int bus = PCI_BUS (dev);
  695. if ((bus == local_buses[0]) || (bus == local_buses[1])) {
  696. *value = pciReadConfigReg ((PCI_HOST) hose->cfg_addr,
  697. offset | (PCI_FUNC(dev) << 8),
  698. PCI_DEV (dev));
  699. } else {
  700. *value = pciOverBridgeReadConfigReg ((PCI_HOST) hose->cfg_addr,
  701. offset | (PCI_FUNC(dev) << 8),
  702. PCI_DEV (dev), bus);
  703. }
  704. return 0;
  705. }
  706. static int gt_write_config_dword (struct pci_controller *hose,
  707. pci_dev_t dev, int offset, u32 value)
  708. {
  709. int bus = PCI_BUS (dev);
  710. if ((bus == local_buses[0]) || (bus == local_buses[1])) {
  711. pciWriteConfigReg ((PCI_HOST) hose->cfg_addr,
  712. offset | (PCI_FUNC(dev) << 8),
  713. PCI_DEV (dev), value);
  714. } else {
  715. pciOverBridgeWriteConfigReg ((PCI_HOST) hose->cfg_addr,
  716. offset | (PCI_FUNC(dev) << 8),
  717. PCI_DEV (dev), bus,
  718. value);
  719. }
  720. return 0;
  721. }
  722. static void gt_setup_ide (struct pci_controller *hose,
  723. pci_dev_t dev, struct pci_config_table *entry)
  724. {
  725. static const int ide_bar[] = { 8, 4, 8, 4, 0, 0 };
  726. u32 bar_response, bar_value;
  727. int bar;
  728. if (CPCI750_SLAVE_TEST != 0)
  729. return;
  730. for (bar = 0; bar < 6; bar++) {
  731. /*ronen different function for 3rd bank. */
  732. unsigned int offset =
  733. (bar < 2) ? bar * 8 : 0x100 + (bar - 2) * 8;
  734. pci_hose_write_config_dword (hose, dev, PCI_BASE_ADDRESS_0 + offset,
  735. 0x0);
  736. pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0 + offset,
  737. &bar_response);
  738. pciauto_region_allocate (bar_response &
  739. PCI_BASE_ADDRESS_SPACE_IO ? hose->
  740. pci_io : hose->pci_mem, ide_bar[bar],
  741. &bar_value);
  742. pci_hose_write_config_dword (hose, dev, PCI_BASE_ADDRESS_0 + bar * 4,
  743. bar_value);
  744. }
  745. }
  746. #ifdef CONFIG_USE_CPCIDVI
  747. static void gt_setup_cpcidvi (struct pci_controller *hose,
  748. pci_dev_t dev, struct pci_config_table *entry)
  749. {
  750. u32 bar_value, pci_response;
  751. if (CPCI750_SLAVE_TEST != 0)
  752. return;
  753. pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &pci_response);
  754. pci_hose_write_config_dword (hose, dev, PCI_BASE_ADDRESS_0, 0xffffffff);
  755. pci_hose_read_config_dword (hose, dev, PCI_BASE_ADDRESS_0, &pci_response);
  756. pciauto_region_allocate (hose->pci_mem, 0x01000000, &bar_value);
  757. pci_hose_write_config_dword (hose, dev, PCI_BASE_ADDRESS_0, (bar_value & 0xffffff00));
  758. pci_hose_write_config_dword (hose, dev, PCI_ROM_ADDRESS, 0x0);
  759. pciauto_region_allocate (hose->pci_mem, 0x40000, &bar_value);
  760. pci_hose_write_config_dword (hose, dev, PCI_ROM_ADDRESS, (bar_value & 0xffffff00) | 0x01);
  761. gt_cpcidvi_rom.base = bar_value & 0xffffff00;
  762. gt_cpcidvi_rom.init = 1;
  763. }
  764. unsigned char gt_cpcidvi_in8(unsigned int offset)
  765. {
  766. unsigned char data;
  767. if (gt_cpcidvi_rom.init == 0) {
  768. return(0);
  769. }
  770. data = in8((offset & 0x04) + 0x3f000 + gt_cpcidvi_rom.base);
  771. return(data);
  772. }
  773. void gt_cpcidvi_out8(unsigned int offset, unsigned char data)
  774. {
  775. unsigned int off;
  776. if (gt_cpcidvi_rom.init == 0) {
  777. return;
  778. }
  779. off = data;
  780. off = ((off << 3) & 0x7f8) + (offset & 0x4) + 0x3e000 + gt_cpcidvi_rom.base;
  781. in8(off);
  782. return;
  783. }
  784. #endif
  785. /* TODO BJW: Change this for DB64360. This was pulled from the EV64260 */
  786. /* and is curently not called *. */
  787. #if 0
  788. static void gt_fixup_irq (struct pci_controller *hose, pci_dev_t dev)
  789. {
  790. unsigned char pin, irq;
  791. pci_read_config_byte (dev, PCI_INTERRUPT_PIN, &pin);
  792. if (pin == 1) { /* only allow INT A */
  793. irq = pci_irq_swizzle[(PCI_HOST) hose->
  794. cfg_addr][PCI_DEV (dev)];
  795. if (irq)
  796. pci_write_config_byte (dev, PCI_INTERRUPT_LINE, irq);
  797. }
  798. }
  799. #endif
  800. struct pci_config_table gt_config_table[] = {
  801. #ifdef CONFIG_USE_CPCIDVI
  802. {PCI_VENDOR_ID_CT, PCI_DEVICE_ID_CT_69030, PCI_CLASS_DISPLAY_VGA,
  803. PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, gt_setup_cpcidvi},
  804. #endif
  805. {PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE,
  806. PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, gt_setup_ide},
  807. {}
  808. };
  809. struct pci_controller pci0_hose = {
  810. /* fixup_irq: gt_fixup_irq, */
  811. config_table:gt_config_table,
  812. };
  813. struct pci_controller pci1_hose = {
  814. /* fixup_irq: gt_fixup_irq, */
  815. config_table:gt_config_table,
  816. };
  817. void pci_init_board (void)
  818. {
  819. unsigned int command;
  820. unsigned int slave;
  821. #ifdef CONFIG_PCI_PNP
  822. unsigned int bar;
  823. #endif
  824. #ifdef DEBUG
  825. gt_pci_bus_mode_display (PCI_HOST0);
  826. #endif
  827. #ifdef CONFIG_USE_CPCIDVI
  828. gt_cpcidvi_rom.init = 0;
  829. gt_cpcidvi_rom.base = 0;
  830. #endif
  831. slave = CPCI750_SLAVE_TEST;
  832. pci0_hose.config_table = gt_config_table;
  833. pci1_hose.config_table = gt_config_table;
  834. #ifdef CONFIG_USE_CPCIDVI
  835. gt_config_table[0].config_device = gt_setup_cpcidvi;
  836. #endif
  837. gt_config_table[1].config_device = gt_setup_ide;
  838. pci0_hose.first_busno = 0;
  839. pci0_hose.last_busno = 0xff;
  840. local_buses[0] = pci0_hose.first_busno;
  841. /* PCI memory space */
  842. pci_set_region (pci0_hose.regions + 0,
  843. CONFIG_SYS_PCI0_0_MEM_SPACE,
  844. CONFIG_SYS_PCI0_0_MEM_SPACE,
  845. CONFIG_SYS_PCI0_MEM_SIZE, PCI_REGION_MEM);
  846. /* PCI I/O space */
  847. pci_set_region (pci0_hose.regions + 1,
  848. CONFIG_SYS_PCI0_IO_SPACE_PCI,
  849. CONFIG_SYS_PCI0_IO_SPACE, CONFIG_SYS_PCI0_IO_SIZE, PCI_REGION_IO);
  850. pci_set_ops (&pci0_hose,
  851. pci_hose_read_config_byte_via_dword,
  852. pci_hose_read_config_word_via_dword,
  853. gt_read_config_dword,
  854. pci_hose_write_config_byte_via_dword,
  855. pci_hose_write_config_word_via_dword,
  856. gt_write_config_dword);
  857. pci0_hose.region_count = 2;
  858. pci0_hose.cfg_addr = (unsigned int *) PCI_HOST0;
  859. pci_register_hose (&pci0_hose);
  860. if (slave == 0) {
  861. pciArbiterEnable (PCI_HOST0);
  862. pciParkingDisable (PCI_HOST0, 1, 1, 1, 1, 1, 1, 1);
  863. command = pciReadConfigReg (PCI_HOST0, PCI_COMMAND, SELF);
  864. command |= PCI_COMMAND_MASTER;
  865. pciWriteConfigReg (PCI_HOST0, PCI_COMMAND, SELF, command);
  866. command = pciReadConfigReg (PCI_HOST0, PCI_COMMAND, SELF);
  867. command |= PCI_COMMAND_MEMORY;
  868. pciWriteConfigReg (PCI_HOST0, PCI_COMMAND, SELF, command);
  869. #ifdef CONFIG_PCI_PNP
  870. pciauto_config_init(&pci0_hose);
  871. pciauto_region_allocate(pci0_hose.pci_io, 0x400, &bar);
  872. #endif
  873. #ifdef CONFIG_PCI_SCAN_SHOW
  874. printf("PCI: Bus Dev VenId DevId Class Int\n");
  875. #endif
  876. pci0_hose.last_busno = pci_hose_scan_bus (&pci0_hose,
  877. pci0_hose.first_busno);
  878. #ifdef DEBUG
  879. gt_pci_bus_mode_display (PCI_HOST1);
  880. #endif
  881. } else {
  882. pciArbiterDisable (PCI_HOST0);
  883. pciParkingDisable (PCI_HOST0, 1, 1, 1, 1, 1, 1, 1);
  884. command = pciReadConfigReg (PCI_HOST0, PCI_COMMAND, SELF);
  885. command |= PCI_COMMAND_MASTER;
  886. pciWriteConfigReg (PCI_HOST0, PCI_COMMAND, SELF, command);
  887. command = pciReadConfigReg (PCI_HOST0, PCI_COMMAND, SELF);
  888. command |= PCI_COMMAND_MEMORY;
  889. pciWriteConfigReg (PCI_HOST0, PCI_COMMAND, SELF, command);
  890. pci0_hose.last_busno = pci0_hose.first_busno;
  891. }
  892. pci1_hose.first_busno = pci0_hose.last_busno + 1;
  893. pci1_hose.last_busno = 0xff;
  894. pci1_hose.current_busno = pci1_hose.first_busno;
  895. local_buses[1] = pci1_hose.first_busno;
  896. /* PCI memory space */
  897. pci_set_region (pci1_hose.regions + 0,
  898. CONFIG_SYS_PCI1_0_MEM_SPACE,
  899. CONFIG_SYS_PCI1_0_MEM_SPACE,
  900. CONFIG_SYS_PCI1_MEM_SIZE, PCI_REGION_MEM);
  901. /* PCI I/O space */
  902. pci_set_region (pci1_hose.regions + 1,
  903. CONFIG_SYS_PCI1_IO_SPACE_PCI,
  904. CONFIG_SYS_PCI1_IO_SPACE, CONFIG_SYS_PCI1_IO_SIZE, PCI_REGION_IO);
  905. pci_set_ops (&pci1_hose,
  906. pci_hose_read_config_byte_via_dword,
  907. pci_hose_read_config_word_via_dword,
  908. gt_read_config_dword,
  909. pci_hose_write_config_byte_via_dword,
  910. pci_hose_write_config_word_via_dword,
  911. gt_write_config_dword);
  912. pci1_hose.region_count = 2;
  913. pci1_hose.cfg_addr = (unsigned int *) PCI_HOST1;
  914. pci_register_hose (&pci1_hose);
  915. pciArbiterEnable (PCI_HOST1);
  916. pciParkingDisable (PCI_HOST1, 1, 1, 1, 1, 1, 1, 1);
  917. command = pciReadConfigReg (PCI_HOST1, PCI_COMMAND, SELF);
  918. command |= PCI_COMMAND_MASTER;
  919. pciWriteConfigReg (PCI_HOST1, PCI_COMMAND, SELF, command);
  920. #ifdef CONFIG_PCI_PNP
  921. pciauto_config_init(&pci1_hose);
  922. pciauto_region_allocate(pci1_hose.pci_io, 0x400, &bar);
  923. #endif
  924. pci1_hose.last_busno = pci_hose_scan_bus (&pci1_hose, pci1_hose.first_busno);
  925. command = pciReadConfigReg (PCI_HOST1, PCI_COMMAND, SELF);
  926. command |= PCI_COMMAND_MEMORY;
  927. pciWriteConfigReg (PCI_HOST1, PCI_COMMAND, SELF, command);
  928. }
  929. #endif /* of CONFIG_PCI */