xref: /openbmc/u-boot/drivers/pci/pci_auto.c (revision 001646c4)
1 /*
2  * arch/powerpc/kernel/pci_auto.c
3  *
4  * PCI autoconfiguration library
5  *
6  * Author: Matt Porter <mporter@mvista.com>
7  *
8  * Copyright 2000 MontaVista Software Inc.
9  *
10  * SPDX-License-Identifier:	GPL-2.0+
11  */
12 
13 #include <common.h>
14 #include <errno.h>
15 #include <pci.h>
16 
17 #undef DEBUG
18 #ifdef DEBUG
19 #define DEBUGF(x...) printf(x)
20 #else
21 #define DEBUGF(x...)
22 #endif /* DEBUG */
23 
24 #define	PCIAUTO_IDE_MODE_MASK		0x05
25 
26 /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
27 #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
28 #define CONFIG_SYS_PCI_CACHE_LINE_SIZE	8
29 #endif
30 
31 /*
32  *
33  */
34 
35 void pciauto_region_init(struct pci_region *res)
36 {
37 	/*
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.
41 	 */
42 	res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
43 }
44 
45 void pciauto_region_align(struct pci_region *res, pci_size_t size)
46 {
47 	res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
48 }
49 
50 int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
51 	pci_addr_t *bar)
52 {
53 	pci_addr_t addr;
54 
55 	if (!res) {
56 		DEBUGF("No resource");
57 		goto error;
58 	}
59 
60 	addr = ((res->bus_lower - 1) | (size - 1)) + 1;
61 
62 	if (addr - res->bus_start + size > res->size) {
63 		DEBUGF("No room in resource");
64 		goto error;
65 	}
66 
67 	res->bus_lower = addr + size;
68 
69 	DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower);
70 
71 	*bar = addr;
72 	return 0;
73 
74  error:
75 	*bar = (pci_addr_t)-1;
76 	return -1;
77 }
78 
79 /*
80  *
81  */
82 
83 void pciauto_setup_device(struct pci_controller *hose,
84 			  pci_dev_t dev, int bars_num,
85 			  struct pci_region *mem,
86 			  struct pci_region *prefetch,
87 			  struct pci_region *io)
88 {
89 	u32 bar_response;
90 	pci_size_t bar_size;
91 	u16 cmdstat = 0;
92 	int bar, bar_nr = 0;
93 #ifndef CONFIG_PCI_ENUM_ONLY
94 	pci_addr_t bar_value;
95 	struct pci_region *bar_res;
96 	int found_mem64 = 0;
97 #endif
98 
99 	pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
100 	cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
101 
102 	for (bar = PCI_BASE_ADDRESS_0;
103 		bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
104 		/* Tickle the BAR and get the response */
105 #ifndef CONFIG_PCI_ENUM_ONLY
106 		pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
107 #endif
108 		pci_hose_read_config_dword(hose, dev, bar, &bar_response);
109 
110 		/* If BAR is not implemented go to the next BAR */
111 		if (!bar_response)
112 			continue;
113 
114 #ifndef CONFIG_PCI_ENUM_ONLY
115 		found_mem64 = 0;
116 #endif
117 
118 		/* Check the BAR type and set our address mask */
119 		if (bar_response & PCI_BASE_ADDRESS_SPACE) {
120 			bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
121 				   & 0xffff) + 1;
122 #ifndef CONFIG_PCI_ENUM_ONLY
123 			bar_res = io;
124 #endif
125 
126 			DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", bar_nr, (u64)bar_size);
127 		} else {
128 			if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
129 			     PCI_BASE_ADDRESS_MEM_TYPE_64) {
130 				u32 bar_response_upper;
131 				u64 bar64;
132 
133 #ifndef CONFIG_PCI_ENUM_ONLY
134 				pci_hose_write_config_dword(hose, dev, bar + 4,
135 					0xffffffff);
136 #endif
137 				pci_hose_read_config_dword(hose, dev, bar + 4,
138 					&bar_response_upper);
139 
140 				bar64 = ((u64)bar_response_upper << 32) | bar_response;
141 
142 				bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
143 #ifndef CONFIG_PCI_ENUM_ONLY
144 				found_mem64 = 1;
145 #endif
146 			} else {
147 				bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
148 			}
149 #ifndef CONFIG_PCI_ENUM_ONLY
150 			if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
151 				bar_res = prefetch;
152 			else
153 				bar_res = mem;
154 #endif
155 
156 			DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%llx, ", bar_nr, (u64)bar_size);
157 		}
158 
159 #ifndef CONFIG_PCI_ENUM_ONLY
160 		if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
161 			/* Write it out and update our limit */
162 			pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value);
163 
164 			if (found_mem64) {
165 				bar += 4;
166 #ifdef CONFIG_SYS_PCI_64BIT
167 				pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
168 #else
169 				/*
170 				 * If we are a 64-bit decoder then increment to the
171 				 * upper 32 bits of the bar and force it to locate
172 				 * in the lower 4GB of memory.
173 				 */
174 				pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
175 #endif
176 			}
177 
178 		}
179 #endif
180 		cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
181 			PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
182 
183 		DEBUGF("\n");
184 
185 		bar_nr++;
186 	}
187 
188 	pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
189 	pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
190 		CONFIG_SYS_PCI_CACHE_LINE_SIZE);
191 	pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
192 }
193 
194 int pciauto_setup_rom(struct pci_controller *hose, pci_dev_t dev)
195 {
196 	pci_addr_t bar_value;
197 	pci_size_t bar_size;
198 	u32 bar_response;
199 	u16 cmdstat = 0;
200 
201 	pci_hose_write_config_dword(hose, dev, PCI_ROM_ADDRESS, 0xfffffffe);
202 	pci_hose_read_config_dword(hose, dev, PCI_ROM_ADDRESS, &bar_response);
203 	if (!bar_response)
204 		return -ENOENT;
205 
206 	bar_size = -(bar_response & ~1);
207 	DEBUGF("PCI Autoconfig: ROM, size=%#x, ", bar_size);
208 	if (pciauto_region_allocate(hose->pci_mem, bar_size, &bar_value) == 0) {
209 		pci_hose_write_config_dword(hose, dev, PCI_ROM_ADDRESS,
210 					    bar_value);
211 	}
212 	DEBUGF("\n");
213 	pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
214 	cmdstat |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
215 	pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
216 
217 	return 0;
218 }
219 
220 void pciauto_prescan_setup_bridge(struct pci_controller *hose,
221 					 pci_dev_t dev, int sub_bus)
222 {
223 	struct pci_region *pci_mem = hose->pci_mem;
224 	struct pci_region *pci_prefetch = hose->pci_prefetch;
225 	struct pci_region *pci_io = hose->pci_io;
226 	u16 cmdstat;
227 
228 	pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
229 
230 	/* Configure bus number registers */
231 	pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
232 				   PCI_BUS(dev) - hose->first_busno);
233 	pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
234 				   sub_bus - hose->first_busno);
235 	pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
236 
237 	if (pci_mem) {
238 		/* Round memory allocator to 1MB boundary */
239 		pciauto_region_align(pci_mem, 0x100000);
240 
241 		/* Set up memory and I/O filter limits, assume 32-bit I/O space */
242 		pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
243 					(pci_mem->bus_lower & 0xfff00000) >> 16);
244 
245 		cmdstat |= PCI_COMMAND_MEMORY;
246 	}
247 
248 	if (pci_prefetch) {
249 		/* Round memory allocator to 1MB boundary */
250 		pciauto_region_align(pci_prefetch, 0x100000);
251 
252 		/* Set up memory and I/O filter limits, assume 32-bit I/O space */
253 		pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
254 					(pci_prefetch->bus_lower & 0xfff00000) >> 16);
255 
256 		cmdstat |= PCI_COMMAND_MEMORY;
257 	} else {
258 		/* We don't support prefetchable memory for now, so disable */
259 		pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
260 		pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
261 	}
262 
263 	if (pci_io) {
264 		/* Round I/O allocator to 4KB boundary */
265 		pciauto_region_align(pci_io, 0x1000);
266 
267 		pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
268 					(pci_io->bus_lower & 0x0000f000) >> 8);
269 		pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
270 					(pci_io->bus_lower & 0xffff0000) >> 16);
271 
272 		cmdstat |= PCI_COMMAND_IO;
273 	}
274 
275 	/* Enable memory and I/O accesses, enable bus master */
276 	pci_hose_write_config_word(hose, dev, PCI_COMMAND,
277 					cmdstat | PCI_COMMAND_MASTER);
278 }
279 
280 void pciauto_postscan_setup_bridge(struct pci_controller *hose,
281 					  pci_dev_t dev, int sub_bus)
282 {
283 	struct pci_region *pci_mem = hose->pci_mem;
284 	struct pci_region *pci_prefetch = hose->pci_prefetch;
285 	struct pci_region *pci_io = hose->pci_io;
286 
287 	/* Configure bus number registers */
288 	pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
289 				   sub_bus - hose->first_busno);
290 
291 	if (pci_mem) {
292 		/* Round memory allocator to 1MB boundary */
293 		pciauto_region_align(pci_mem, 0x100000);
294 
295 		pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
296 				(pci_mem->bus_lower - 1) >> 16);
297 	}
298 
299 	if (pci_prefetch) {
300 		/* Round memory allocator to 1MB boundary */
301 		pciauto_region_align(pci_prefetch, 0x100000);
302 
303 		pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
304 				(pci_prefetch->bus_lower - 1) >> 16);
305 	}
306 
307 	if (pci_io) {
308 		/* Round I/O allocator to 4KB boundary */
309 		pciauto_region_align(pci_io, 0x1000);
310 
311 		pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
312 				((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
313 		pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
314 				((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
315 	}
316 }
317 
318 /*
319  *
320  */
321 
322 void pciauto_config_init(struct pci_controller *hose)
323 {
324 	int i;
325 
326 	hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
327 
328 	for (i = 0; i < hose->region_count; i++) {
329 		switch(hose->regions[i].flags) {
330 		case PCI_REGION_IO:
331 			if (!hose->pci_io ||
332 			    hose->pci_io->size < hose->regions[i].size)
333 				hose->pci_io = hose->regions + i;
334 			break;
335 		case PCI_REGION_MEM:
336 			if (!hose->pci_mem ||
337 			    hose->pci_mem->size < hose->regions[i].size)
338 				hose->pci_mem = hose->regions + i;
339 			break;
340 		case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
341 			if (!hose->pci_prefetch ||
342 			    hose->pci_prefetch->size < hose->regions[i].size)
343 				hose->pci_prefetch = hose->regions + i;
344 			break;
345 		}
346 	}
347 
348 
349 	if (hose->pci_mem) {
350 		pciauto_region_init(hose->pci_mem);
351 
352 		DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
353 		       "\t\tPhysical Memory [%llx-%llxx]\n",
354 		    (u64)hose->pci_mem->bus_start,
355 		    (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
356 		    (u64)hose->pci_mem->phys_start,
357 		    (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
358 	}
359 
360 	if (hose->pci_prefetch) {
361 		pciauto_region_init(hose->pci_prefetch);
362 
363 		DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
364 		       "\t\tPhysical Memory [%llx-%llx]\n",
365 		    (u64)hose->pci_prefetch->bus_start,
366 		    (u64)(hose->pci_prefetch->bus_start +
367 			    hose->pci_prefetch->size - 1),
368 		    (u64)hose->pci_prefetch->phys_start,
369 		    (u64)(hose->pci_prefetch->phys_start +
370 			    hose->pci_prefetch->size - 1));
371 	}
372 
373 	if (hose->pci_io) {
374 		pciauto_region_init(hose->pci_io);
375 
376 		DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
377 		       "\t\tPhysical Memory: [%llx-%llx]\n",
378 		    (u64)hose->pci_io->bus_start,
379 		    (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
380 		    (u64)hose->pci_io->phys_start,
381 		    (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
382 
383 	}
384 }
385 
386 /*
387  * HJF: Changed this to return int. I think this is required
388  * to get the correct result when scanning bridges
389  */
390 int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
391 {
392 	unsigned int sub_bus = PCI_BUS(dev);
393 	unsigned short class;
394 	unsigned char prg_iface;
395 	int n;
396 
397 	pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
398 
399 	switch (class) {
400 	case PCI_CLASS_BRIDGE_PCI:
401 		hose->current_busno++;
402 		pciauto_setup_device(hose, dev, 2, hose->pci_mem,
403 			hose->pci_prefetch, hose->pci_io);
404 
405 		DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev));
406 
407 		/* Passing in current_busno allows for sibling P2P bridges */
408 		pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
409 		/*
410 		 * need to figure out if this is a subordinate bridge on the bus
411 		 * to be able to properly set the pri/sec/sub bridge registers.
412 		 */
413 		n = pci_hose_scan_bus(hose, hose->current_busno);
414 
415 		/* figure out the deepest we've gone for this leg */
416 		sub_bus = max((unsigned int)n, sub_bus);
417 		pciauto_postscan_setup_bridge(hose, dev, sub_bus);
418 
419 		sub_bus = hose->current_busno;
420 		break;
421 
422 	case PCI_CLASS_STORAGE_IDE:
423 		pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface);
424 		if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
425 			DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
426 			return sub_bus;
427 		}
428 
429 		pciauto_setup_device(hose, dev, 6, hose->pci_mem,
430 			hose->pci_prefetch, hose->pci_io);
431 		break;
432 
433 	case PCI_CLASS_BRIDGE_CARDBUS:
434 		/*
435 		 * just do a minimal setup of the bridge,
436 		 * let the OS take care of the rest
437 		 */
438 		pciauto_setup_device(hose, dev, 0, hose->pci_mem,
439 			hose->pci_prefetch, hose->pci_io);
440 
441 		DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
442 			PCI_DEV(dev));
443 
444 		hose->current_busno++;
445 		break;
446 
447 #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
448 	case PCI_CLASS_BRIDGE_OTHER:
449 		DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
450 		       PCI_DEV(dev));
451 		break;
452 #endif
453 #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
454 	case PCI_CLASS_BRIDGE_OTHER:
455 		/*
456 		 * The host/PCI bridge 1 seems broken in 8349 - it presents
457 		 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
458 		 * device claiming resources io/mem/irq.. we only allow for
459 		 * the PIMMR window to be allocated (BAR0 - 1MB size)
460 		 */
461 		DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
462 		pciauto_setup_device(hose, dev, 0, hose->pci_mem,
463 			hose->pci_prefetch, hose->pci_io);
464 		break;
465 #endif
466 
467 	case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
468 		DEBUGF("PCI AutoConfig: Found PowerPC device\n");
469 
470 	default:
471 		pciauto_setup_device(hose, dev, 6, hose->pci_mem,
472 			hose->pci_prefetch, hose->pci_io);
473 		break;
474 	}
475 
476 	return sub_bus;
477 }
478