pci.c 32 KB

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