1 | /* PCI (Static) Configuration Library |
---|
2 | * |
---|
3 | * COPYRIGHT (c) 2010 Cobham Gaisler AB. |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * The Host Bridge bus must be declared by user. It contains the static |
---|
12 | * configuration used to setup the devices/functions. |
---|
13 | */ |
---|
14 | |
---|
15 | /* Configure headers */ |
---|
16 | #define PCI_CFG_STATIC_LIB |
---|
17 | |
---|
18 | #include <stdlib.h> |
---|
19 | #include <pci.h> |
---|
20 | #include <pci/access.h> |
---|
21 | #include <pci/cfg.h> |
---|
22 | |
---|
23 | #define PCI_CFG_R8(dev, args...) pci_cfg_r8(dev, args) |
---|
24 | #define PCI_CFG_R16(dev, args...) pci_cfg_r16(dev, args) |
---|
25 | #define PCI_CFG_R32(dev, args...) pci_cfg_r32(dev, args) |
---|
26 | #define PCI_CFG_W8(dev, args...) pci_cfg_w8(dev, args) |
---|
27 | #define PCI_CFG_W16(dev, args...) pci_cfg_w16(dev, args) |
---|
28 | #define PCI_CFG_W32(dev, args...) pci_cfg_w32(dev, args) |
---|
29 | |
---|
30 | /* Number of buses */ |
---|
31 | extern int pci_bus_cnt; |
---|
32 | |
---|
33 | /* Enumrate one bus if device is a bridge, and all it's subordinate buses */ |
---|
34 | static int pci_init_dev(struct pci_dev *dev, void *unused) |
---|
35 | { |
---|
36 | uint32_t tmp; |
---|
37 | uint16_t tmp16, cmd; |
---|
38 | struct pci_bus *bridge; |
---|
39 | int maxbars, i, romofs; |
---|
40 | pci_dev_t pcidev = dev->busdevfun; |
---|
41 | struct pci_res *res; |
---|
42 | |
---|
43 | /* Init Device */ |
---|
44 | |
---|
45 | /* Set command to reset values, it disables bus |
---|
46 | * mastering and address responses. |
---|
47 | */ |
---|
48 | PCI_CFG_W16(pcidev, PCIR_COMMAND, 0); |
---|
49 | |
---|
50 | /* Clear any already set status bits */ |
---|
51 | PCI_CFG_W16(pcidev, PCIR_STATUS, 0xf900); |
---|
52 | |
---|
53 | /* Set latency timer to 64 */ |
---|
54 | PCI_CFG_W8(pcidev, PCIR_LATTIMER, 64); |
---|
55 | |
---|
56 | /* Set System IRQ of PIN */ |
---|
57 | PCI_CFG_W8(pcidev, PCIR_INTLINE, dev->sysirq); |
---|
58 | |
---|
59 | cmd = dev->command; |
---|
60 | |
---|
61 | if ((dev->flags & PCI_DEV_BRIDGE) == 0) { |
---|
62 | /* Disable Cardbus CIS Pointer */ |
---|
63 | PCI_CFG_W32(pcidev, PCIR_CIS, 0); |
---|
64 | |
---|
65 | romofs = PCIR_BIOS; |
---|
66 | maxbars = 6; |
---|
67 | } else { |
---|
68 | /* Init Bridge */ |
---|
69 | |
---|
70 | /* Configure bridge (no support for 64-bit) */ |
---|
71 | PCI_CFG_W32(pcidev, PCIR_PMBASEH_1, 0); |
---|
72 | PCI_CFG_W32(pcidev, PCIR_PMLIMITH_1, 0); |
---|
73 | |
---|
74 | bridge = (struct pci_bus *)dev; |
---|
75 | tmp = (64 << 24) | (bridge->sord << 16) | |
---|
76 | (bridge->num << 8) | bridge->pri; |
---|
77 | PCI_CFG_W32(pcidev, PCIR_PRIBUS_1, tmp); |
---|
78 | |
---|
79 | /*** Setup I/O Bridge Window ***/ |
---|
80 | res = &dev->resources[BRIDGE_RES_IO]; |
---|
81 | if (res->size > 0) { |
---|
82 | tmp16 = ((res->end-1) & 0x0000f000) | |
---|
83 | ((res->start & 0x0000f000) >> 8); |
---|
84 | tmp = ((res->end-1) & 0xffff0000) | (res->start >> 16); |
---|
85 | cmd |= PCIM_CMD_PORTEN; |
---|
86 | } else { |
---|
87 | tmp16 = 0x00ff; |
---|
88 | tmp = 0; |
---|
89 | } |
---|
90 | /* I/O Limit and Base */ |
---|
91 | PCI_CFG_W16(pcidev, PCIR_IOBASEL_1, tmp16); |
---|
92 | PCI_CFG_W32(pcidev, PCIR_IOBASEH_1, tmp); |
---|
93 | |
---|
94 | /*** Setup MEMIO Bridge Window ***/ |
---|
95 | res = &dev->resources[BRIDGE_RES_MEMIO]; |
---|
96 | if (res->size > 0) { |
---|
97 | tmp = ((res->end-1) & 0xffff0000) | |
---|
98 | (res->start >> 16); |
---|
99 | cmd |= PCIM_CMD_MEMEN; |
---|
100 | } else { |
---|
101 | tmp = 0x0000ffff; |
---|
102 | } |
---|
103 | /* MEMIO Limit and Base */ |
---|
104 | PCI_CFG_W32(pcidev, PCIR_MEMBASE_1, tmp); |
---|
105 | |
---|
106 | /*** Setup MEM Bridge Window ***/ |
---|
107 | res = &dev->resources[BRIDGE_RES_MEM]; |
---|
108 | if (res->size > 0) { |
---|
109 | tmp = ((res->end-1) & 0xffff0000) | |
---|
110 | (res->start >> 16); |
---|
111 | cmd |= PCIM_CMD_MEMEN; |
---|
112 | } else { |
---|
113 | tmp = 0x0000ffff; |
---|
114 | } |
---|
115 | /* MEM Limit and Base */ |
---|
116 | PCI_CFG_W32(pcidev, PCIR_PMBASEL_1, tmp); |
---|
117 | /* 64-bit space not supported */ |
---|
118 | PCI_CFG_W32(pcidev, PCIR_PMBASEH_1, 0); |
---|
119 | PCI_CFG_W32(pcidev, PCIR_PMLIMITH_1, 0); |
---|
120 | |
---|
121 | cmd |= PCIM_CMD_BUSMASTEREN; |
---|
122 | romofs = PCIR_BIOS_1; |
---|
123 | maxbars = 2; |
---|
124 | } |
---|
125 | |
---|
126 | /* Init BARs */ |
---|
127 | for (i = 0; i < maxbars; i++) { |
---|
128 | res = &dev->resources[i]; |
---|
129 | if (res->flags & PCI_RES_TYPE_MASK) { |
---|
130 | PCI_CFG_W32(pcidev, PCIR_BAR(0) + 4*i, |
---|
131 | res->start); |
---|
132 | if ((res->flags & PCI_RES_TYPE_MASK) == PCI_RES_IO) |
---|
133 | cmd |= PCIM_CMD_PORTEN; |
---|
134 | else |
---|
135 | cmd |= PCIM_CMD_MEMEN; |
---|
136 | } |
---|
137 | } |
---|
138 | res = &dev->resources[DEV_RES_ROM]; |
---|
139 | if (res->flags & PCI_RES_TYPE_MASK) { |
---|
140 | PCI_CFG_W32(pcidev, romofs, res->start|PCIM_BIOS_ENABLE); |
---|
141 | cmd |= PCIM_CMD_MEMEN; |
---|
142 | } |
---|
143 | PCI_CFG_W16(pcidev, PCIR_COMMAND, cmd); |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | /* Assume that user has defined static setup array in pci_hb */ |
---|
149 | int pci_config_static(void) |
---|
150 | { |
---|
151 | pci_bus_cnt = pci_hb.sord + 1; |
---|
152 | pci_system_type = PCI_SYSTEM_HOST; |
---|
153 | |
---|
154 | /* Init all PCI devices according to depth-first search algorithm */ |
---|
155 | return pci_for_each_dev(pci_init_dev, NULL); |
---|
156 | } |
---|