2 * arch/ppc/kernel/pci_auto.c
4 * PCI autoconfiguration library
6 * Author: Matt Porter <mporter@mvista.com>
8 * Copyright 2000 MontaVista Software Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
24 #define DEBUGF(x...) printf(x)
29 #define PCIAUTO_IDE_MODE_MASK 0x05
35 void pciauto_region_init(struct pci_region
* res
)
38 * Avoid allocating PCI resources from address 0 -- this is illegal
39 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
40 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
42 res
->bus_lower
= res
->bus_start
? res
->bus_start
: 0x1000;
45 void pciauto_region_align(struct pci_region
*res
, unsigned long size
)
47 res
->bus_lower
= ((res
->bus_lower
- 1) | (size
- 1)) + 1;
50 int pciauto_region_allocate(struct pci_region
* res
, unsigned int size
, unsigned int *bar
)
55 DEBUGF("No resource");
59 addr
= ((res
->bus_lower
- 1) | (size
- 1)) + 1;
61 if (addr
- res
->bus_start
+ size
> res
->size
) {
62 DEBUGF("No room in resource");
66 res
->bus_lower
= addr
+ size
;
68 DEBUGF("address=0x%lx", addr
);
82 void pciauto_setup_device(struct pci_controller
*hose
,
83 pci_dev_t dev
, int bars_num
,
84 struct pci_region
*mem
,
85 struct pci_region
*prefetch
,
86 struct pci_region
*io
)
88 unsigned int bar_value
, bar_response
, bar_size
;
89 unsigned int cmdstat
= 0;
90 struct pci_region
*bar_res
;
94 pci_hose_read_config_dword(hose
, dev
, PCI_COMMAND
, &cmdstat
);
95 cmdstat
= (cmdstat
& ~(PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
)) | PCI_COMMAND_MASTER
;
97 for (bar
= PCI_BASE_ADDRESS_0
; bar
<= PCI_BASE_ADDRESS_0
+ (bars_num
*4); bar
+= 4) {
98 /* Tickle the BAR and get the response */
99 pci_hose_write_config_dword(hose
, dev
, bar
, 0xffffffff);
100 pci_hose_read_config_dword(hose
, dev
, bar
, &bar_response
);
102 /* If BAR is not implemented go to the next BAR */
108 /* Check the BAR type and set our address mask */
109 if (bar_response
& PCI_BASE_ADDRESS_SPACE
) {
110 bar_size
= ((~(bar_response
& PCI_BASE_ADDRESS_IO_MASK
))
114 DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%x, ", bar_nr
, bar_size
);
116 if ( (bar_response
& PCI_BASE_ADDRESS_MEM_TYPE_MASK
) ==
117 PCI_BASE_ADDRESS_MEM_TYPE_64
)
120 bar_size
= ~(bar_response
& PCI_BASE_ADDRESS_MEM_MASK
) + 1;
121 if (prefetch
&& (bar_response
& PCI_BASE_ADDRESS_MEM_PREFETCH
))
126 DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%x, ", bar_nr
, bar_size
);
129 if (pciauto_region_allocate(bar_res
, bar_size
, &bar_value
) == 0) {
130 /* Write it out and update our limit */
131 pci_hose_write_config_dword(hose
, dev
, bar
, bar_value
);
134 * If we are a 64-bit decoder then increment to the
135 * upper 32 bits of the bar and force it to locate
136 * in the lower 4GB of memory.
140 pci_hose_write_config_dword(hose
, dev
, bar
, 0x00000000);
143 cmdstat
|= (bar_response
& PCI_BASE_ADDRESS_SPACE
) ?
144 PCI_COMMAND_IO
: PCI_COMMAND_MEMORY
;
152 pci_hose_write_config_dword(hose
, dev
, PCI_COMMAND
, cmdstat
);
153 pci_hose_write_config_byte(hose
, dev
, PCI_CACHE_LINE_SIZE
, 0x08);
154 pci_hose_write_config_byte(hose
, dev
, PCI_LATENCY_TIMER
, 0x80);
157 static void pciauto_prescan_setup_bridge(struct pci_controller
*hose
,
158 pci_dev_t dev
, int sub_bus
)
160 struct pci_region
*pci_mem
= hose
->pci_mem
;
161 struct pci_region
*pci_prefetch
= hose
->pci_prefetch
;
162 struct pci_region
*pci_io
= hose
->pci_io
;
163 unsigned int cmdstat
;
165 pci_hose_read_config_dword(hose
, dev
, PCI_COMMAND
, &cmdstat
);
167 /* Configure bus number registers */
168 pci_hose_write_config_byte(hose
, dev
, PCI_PRIMARY_BUS
, PCI_BUS(dev
));
169 pci_hose_write_config_byte(hose
, dev
, PCI_SECONDARY_BUS
, sub_bus
);
170 pci_hose_write_config_byte(hose
, dev
, PCI_SUBORDINATE_BUS
, 0xff);
173 /* Round memory allocator to 1MB boundary */
174 pciauto_region_align(pci_mem
, 0x100000);
176 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
177 pci_hose_write_config_word(hose
, dev
, PCI_MEMORY_BASE
,
178 (pci_mem
->bus_lower
& 0xfff00000) >> 16);
180 cmdstat
|= PCI_COMMAND_MEMORY
;
184 /* Round memory allocator to 1MB boundary */
185 pciauto_region_align(pci_prefetch
, 0x100000);
187 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
188 pci_hose_write_config_word(hose
, dev
, PCI_PREF_MEMORY_BASE
,
189 (pci_prefetch
->bus_lower
& 0xfff00000) >> 16);
191 cmdstat
|= PCI_COMMAND_MEMORY
;
193 /* We don't support prefetchable memory for now, so disable */
194 pci_hose_write_config_word(hose
, dev
, PCI_PREF_MEMORY_BASE
, 0x1000);
195 pci_hose_write_config_word(hose
, dev
, PCI_PREF_MEMORY_LIMIT
, 0x0);
199 /* Round I/O allocator to 4KB boundary */
200 pciauto_region_align(pci_io
, 0x1000);
202 pci_hose_write_config_byte(hose
, dev
, PCI_IO_BASE
,
203 (pci_io
->bus_lower
& 0x0000f000) >> 8);
204 pci_hose_write_config_word(hose
, dev
, PCI_IO_BASE_UPPER16
,
205 (pci_io
->bus_lower
& 0xffff0000) >> 16);
207 cmdstat
|= PCI_COMMAND_IO
;
210 /* Enable memory and I/O accesses, enable bus master */
211 pci_hose_write_config_dword(hose
, dev
, PCI_COMMAND
, cmdstat
| PCI_COMMAND_MASTER
);
214 static void pciauto_postscan_setup_bridge(struct pci_controller
*hose
,
215 pci_dev_t dev
, int sub_bus
)
217 struct pci_region
*pci_mem
= hose
->pci_mem
;
218 struct pci_region
*pci_prefetch
= hose
->pci_prefetch
;
219 struct pci_region
*pci_io
= hose
->pci_io
;
221 /* Configure bus number registers */
222 pci_hose_write_config_byte(hose
, dev
, PCI_SUBORDINATE_BUS
, sub_bus
);
225 /* Round memory allocator to 1MB boundary */
226 pciauto_region_align(pci_mem
, 0x100000);
228 pci_hose_write_config_word(hose
, dev
, PCI_MEMORY_LIMIT
,
229 (pci_mem
->bus_lower
-1) >> 16);
233 /* Round memory allocator to 1MB boundary */
234 pciauto_region_align(pci_prefetch
, 0x100000);
236 pci_hose_write_config_word(hose
, dev
, PCI_PREF_MEMORY_LIMIT
,
237 (pci_prefetch
->bus_lower
-1) >> 16);
241 /* Round I/O allocator to 4KB boundary */
242 pciauto_region_align(pci_io
, 0x1000);
244 pci_hose_write_config_byte(hose
, dev
, PCI_IO_LIMIT
,
245 ((pci_io
->bus_lower
-1) & 0x0000f000) >> 8);
246 pci_hose_write_config_word(hose
, dev
, PCI_IO_LIMIT_UPPER16
,
247 ((pci_io
->bus_lower
-1) & 0xffff0000) >> 16);
255 void pciauto_config_init(struct pci_controller
*hose
)
259 hose
->pci_io
= hose
->pci_mem
= NULL
;
261 for (i
=0; i
<hose
->region_count
; i
++) {
262 switch(hose
->regions
[i
].flags
) {
265 hose
->pci_io
->size
< hose
->regions
[i
].size
)
266 hose
->pci_io
= hose
->regions
+ i
;
269 if (!hose
->pci_mem
||
270 hose
->pci_mem
->size
< hose
->regions
[i
].size
)
271 hose
->pci_mem
= hose
->regions
+ i
;
273 case (PCI_REGION_MEM
| PCI_REGION_PREFETCH
):
274 if (!hose
->pci_prefetch
||
275 hose
->pci_prefetch
->size
< hose
->regions
[i
].size
)
276 hose
->pci_prefetch
= hose
->regions
+ i
;
283 pciauto_region_init(hose
->pci_mem
);
285 DEBUGF("PCI Autoconfig: Memory region: [%lx-%lx]\n",
286 hose
->pci_mem
->bus_start
,
287 hose
->pci_mem
->bus_start
+ hose
->pci_mem
->size
- 1);
290 if (hose
->pci_prefetch
) {
291 pciauto_region_init(hose
->pci_prefetch
);
293 DEBUGF("PCI Autoconfig: Prefetchable Memory region: [%lx-%lx]\n",
294 hose
->pci_prefetch
->bus_start
,
295 hose
->pci_prefetch
->bus_start
+ hose
->pci_prefetch
->size
- 1);
299 pciauto_region_init(hose
->pci_io
);
301 DEBUGF("PCI Autoconfig: I/O region: [%lx-%lx]\n",
302 hose
->pci_io
->bus_start
,
303 hose
->pci_io
->bus_start
+ hose
->pci_io
->size
- 1);
307 /* HJF: Changed this to return int. I think this is required
308 * to get the correct result when scanning bridges
310 int pciauto_config_device(struct pci_controller
*hose
, pci_dev_t dev
)
312 unsigned int sub_bus
= PCI_BUS(dev
);
313 unsigned short class;
314 unsigned char prg_iface
;
317 pci_hose_read_config_word(hose
, dev
, PCI_CLASS_DEVICE
, &class);
320 case PCI_CLASS_BRIDGE_PCI
:
321 hose
->current_busno
++;
322 pciauto_setup_device(hose
, dev
, 2, hose
->pci_mem
, hose
->pci_prefetch
, hose
->pci_io
);
324 DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev
));
326 /* Passing in current_busno allows for sibling P2P bridges */
327 pciauto_prescan_setup_bridge(hose
, dev
, hose
->current_busno
);
329 * need to figure out if this is a subordinate bridge on the bus
330 * to be able to properly set the pri/sec/sub bridge registers.
332 n
= pci_hose_scan_bus(hose
, hose
->current_busno
);
334 /* figure out the deepest we've gone for this leg */
335 sub_bus
= max(n
, sub_bus
);
336 pciauto_postscan_setup_bridge(hose
, dev
, sub_bus
);
338 sub_bus
= hose
->current_busno
;
341 case PCI_CLASS_STORAGE_IDE
:
342 pci_hose_read_config_byte(hose
, dev
, PCI_CLASS_PROG
, &prg_iface
);
343 if (!(prg_iface
& PCIAUTO_IDE_MODE_MASK
)) {
344 DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
348 pciauto_setup_device(hose
, dev
, 6, hose
->pci_mem
, hose
->pci_prefetch
, hose
->pci_io
);
351 case PCI_CLASS_BRIDGE_CARDBUS
:
352 /* just do a minimal setup of the bridge, let the OS take care of the rest */
353 pciauto_setup_device(hose
, dev
, 0, hose
->pci_mem
, hose
->pci_prefetch
, hose
->pci_io
);
355 DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n", PCI_DEV(dev
));
357 hose
->current_busno
++;
360 #ifdef CONFIG_MPC5200
361 case PCI_CLASS_BRIDGE_OTHER
:
362 DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
366 #ifdef CONFIG_MPC834X
367 case PCI_CLASS_BRIDGE_OTHER
:
369 * The host/PCI bridge 1 seems broken in 8349 - it presents
370 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
371 * device claiming resources io/mem/irq.. we only allow for
372 * the PIMMR window to be allocated (BAR0 - 1MB size)
374 DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
375 pciauto_setup_device(hose
, dev
, 0, hose
->pci_mem
, hose
->pci_prefetch
, hose
->pci_io
);
379 pciauto_setup_device(hose
, dev
, 6, hose
->pci_mem
, hose
->pci_prefetch
, hose
->pci_io
);
386 #endif /* CONFIG_PCI */