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