blob: 2acf9bf780802a8aa16b78a72e2ca48f2a7059e5 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * arch/ppc/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 * 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.
14 */
15
16#include <common.h>
17
wdenkc6097192002-11-03 00:24:07 +000018#include <pci.h>
19
20#undef DEBUG
21#ifdef DEBUG
22#define DEBUGF(x...) printf(x)
23#else
24#define DEBUGF(x...)
25#endif /* DEBUG */
26
27#define PCIAUTO_IDE_MODE_MASK 0x05
28
Gary Jennejohn9a1263f2007-08-31 15:21:46 +020029/* the user can define CFG_PCI_CACHE_LINE_SIZE to avoid problems */
30#ifndef CFG_PCI_CACHE_LINE_SIZE
31#define CFG_PCI_CACHE_LINE_SIZE 8
32#endif
33
wdenkc6097192002-11-03 00:24:07 +000034/*
35 *
36 */
37
38void pciauto_region_init(struct pci_region* res)
39{
Sergei Shtylyov9679f4d2007-04-23 15:30:39 +020040 /*
41 * Avoid allocating PCI resources from address 0 -- this is illegal
42 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
43 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
44 */
45 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
wdenkc6097192002-11-03 00:24:07 +000046}
47
48void pciauto_region_align(struct pci_region *res, unsigned long size)
49{
50 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
51}
52
53int pciauto_region_allocate(struct pci_region* res, unsigned int size, unsigned int *bar)
54{
55 unsigned long addr;
56
wdenk56ed43e2004-02-22 23:46:08 +000057 if (!res) {
wdenkc6097192002-11-03 00:24:07 +000058 DEBUGF("No resource");
59 goto error;
60 }
61
62 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
63
wdenk56ed43e2004-02-22 23:46:08 +000064 if (addr - res->bus_start + size > res->size) {
wdenkc6097192002-11-03 00:24:07 +000065 DEBUGF("No room in resource");
66 goto error;
67 }
68
69 res->bus_lower = addr + size;
70
Ed Swarthouta5232962007-07-11 14:51:48 -050071 DEBUGF("address=0x%lx bus_lower=%x", addr, res->bus_lower);
wdenkc6097192002-11-03 00:24:07 +000072
73 *bar = addr;
74 return 0;
75
76 error:
77 *bar = 0xffffffff;
78 return -1;
79}
80
81/*
82 *
83 */
84
85void pciauto_setup_device(struct pci_controller *hose,
86 pci_dev_t dev, int bars_num,
87 struct pci_region *mem,
Kumar Galae5ce4202006-01-11 13:24:15 -060088 struct pci_region *prefetch,
wdenkc6097192002-11-03 00:24:07 +000089 struct pci_region *io)
90{
91 unsigned int bar_value, bar_response, bar_size;
92 unsigned int cmdstat = 0;
93 struct pci_region *bar_res;
94 int bar, bar_nr = 0;
95 int found_mem64 = 0;
96
97 pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat);
98 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
99
Ed Swarthoutea8949e2007-07-27 01:50:44 -0500100 for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_0 + (bars_num*4); bar += 4) {
wdenkc6097192002-11-03 00:24:07 +0000101 /* Tickle the BAR and get the response */
102 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
103 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
104
105 /* If BAR is not implemented go to the next BAR */
106 if (!bar_response)
107 continue;
108
109 found_mem64 = 0;
110
111 /* Check the BAR type and set our address mask */
wdenk56ed43e2004-02-22 23:46:08 +0000112 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Jin Zhengxiong-R64188f4ff3e82006-06-27 18:12:02 +0800113 bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
114 & 0xffff) + 1;
wdenkc6097192002-11-03 00:24:07 +0000115 bar_res = io;
116
117 DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%x, ", bar_nr, bar_size);
wdenk56ed43e2004-02-22 23:46:08 +0000118 } else {
wdenkc6097192002-11-03 00:24:07 +0000119 if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
120 PCI_BASE_ADDRESS_MEM_TYPE_64)
121 found_mem64 = 1;
122
123 bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1;
Kumar Galae5ce4202006-01-11 13:24:15 -0600124 if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
125 bar_res = prefetch;
126 else
127 bar_res = mem;
wdenkc6097192002-11-03 00:24:07 +0000128
129 DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%x, ", bar_nr, bar_size);
130 }
131
wdenk56ed43e2004-02-22 23:46:08 +0000132 if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000133 /* Write it out and update our limit */
134 pci_hose_write_config_dword(hose, dev, bar, bar_value);
135
136 /*
137 * If we are a 64-bit decoder then increment to the
138 * upper 32 bits of the bar and force it to locate
139 * in the lower 4GB of memory.
140 */
wdenk56ed43e2004-02-22 23:46:08 +0000141 if (found_mem64) {
wdenkc6097192002-11-03 00:24:07 +0000142 bar += 4;
143 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
144 }
145
146 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
147 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
148 }
149
150 DEBUGF("\n");
151
152 bar_nr++;
153 }
154
155 pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat);
Gary Jennejohn9a1263f2007-08-31 15:21:46 +0200156 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
157 CFG_PCI_CACHE_LINE_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000158 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
159}
160
Ed Swarthouta5232962007-07-11 14:51:48 -0500161void pciauto_prescan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000162 pci_dev_t dev, int sub_bus)
163{
164 struct pci_region *pci_mem = hose->pci_mem;
Kumar Galae5ce4202006-01-11 13:24:15 -0600165 struct pci_region *pci_prefetch = hose->pci_prefetch;
wdenkc6097192002-11-03 00:24:07 +0000166 struct pci_region *pci_io = hose->pci_io;
167 unsigned int cmdstat;
168
169 pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat);
170
171 /* Configure bus number registers */
Ed Swarthout4aeb55a2007-07-11 14:52:08 -0500172 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
173 PCI_BUS(dev) - hose->first_busno);
174 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
175 sub_bus - hose->first_busno);
wdenkc6097192002-11-03 00:24:07 +0000176 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
177
wdenk56ed43e2004-02-22 23:46:08 +0000178 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000179 /* Round memory allocator to 1MB boundary */
180 pciauto_region_align(pci_mem, 0x100000);
181
182 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
183 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
184 (pci_mem->bus_lower & 0xfff00000) >> 16);
185
186 cmdstat |= PCI_COMMAND_MEMORY;
187 }
188
Kumar Galae5ce4202006-01-11 13:24:15 -0600189 if (pci_prefetch) {
190 /* Round memory allocator to 1MB boundary */
191 pciauto_region_align(pci_prefetch, 0x100000);
192
193 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
194 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
195 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
196
197 cmdstat |= PCI_COMMAND_MEMORY;
198 } else {
199 /* We don't support prefetchable memory for now, so disable */
200 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
Matthew McClintock2f43f332006-06-28 10:44:23 -0500201 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
Kumar Galae5ce4202006-01-11 13:24:15 -0600202 }
203
wdenk56ed43e2004-02-22 23:46:08 +0000204 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000205 /* Round I/O allocator to 4KB boundary */
206 pciauto_region_align(pci_io, 0x1000);
207
208 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
209 (pci_io->bus_lower & 0x0000f000) >> 8);
210 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
211 (pci_io->bus_lower & 0xffff0000) >> 16);
212
213 cmdstat |= PCI_COMMAND_IO;
214 }
215
wdenkc6097192002-11-03 00:24:07 +0000216 /* Enable memory and I/O accesses, enable bus master */
217 pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
218}
219
Ed Swarthouta5232962007-07-11 14:51:48 -0500220void pciauto_postscan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000221 pci_dev_t dev, int sub_bus)
222{
223 struct pci_region *pci_mem = hose->pci_mem;
Kumar Galae5ce4202006-01-11 13:24:15 -0600224 struct pci_region *pci_prefetch = hose->pci_prefetch;
wdenkc6097192002-11-03 00:24:07 +0000225 struct pci_region *pci_io = hose->pci_io;
226
227 /* Configure bus number registers */
Ed Swarthout4aeb55a2007-07-11 14:52:08 -0500228 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
229 sub_bus - hose->first_busno);
wdenkc6097192002-11-03 00:24:07 +0000230
wdenk56ed43e2004-02-22 23:46:08 +0000231 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000232 /* Round memory allocator to 1MB boundary */
233 pciauto_region_align(pci_mem, 0x100000);
234
235 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
236 (pci_mem->bus_lower-1) >> 16);
237 }
238
Kumar Galae5ce4202006-01-11 13:24:15 -0600239 if (pci_prefetch) {
240 /* Round memory allocator to 1MB boundary */
241 pciauto_region_align(pci_prefetch, 0x100000);
242
243 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
244 (pci_prefetch->bus_lower-1) >> 16);
245 }
246
wdenk56ed43e2004-02-22 23:46:08 +0000247 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000248 /* Round I/O allocator to 4KB boundary */
249 pciauto_region_align(pci_io, 0x1000);
250
251 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
252 ((pci_io->bus_lower-1) & 0x0000f000) >> 8);
253 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
254 ((pci_io->bus_lower-1) & 0xffff0000) >> 16);
255 }
256}
257
258/*
259 *
260 */
261
262void pciauto_config_init(struct pci_controller *hose)
263{
264 int i;
265
266 hose->pci_io = hose->pci_mem = NULL;
267
wdenk56ed43e2004-02-22 23:46:08 +0000268 for (i=0; i<hose->region_count; i++) {
269 switch(hose->regions[i].flags) {
wdenkc6097192002-11-03 00:24:07 +0000270 case PCI_REGION_IO:
271 if (!hose->pci_io ||
272 hose->pci_io->size < hose->regions[i].size)
273 hose->pci_io = hose->regions + i;
274 break;
275 case PCI_REGION_MEM:
276 if (!hose->pci_mem ||
277 hose->pci_mem->size < hose->regions[i].size)
278 hose->pci_mem = hose->regions + i;
279 break;
Kumar Galae5ce4202006-01-11 13:24:15 -0600280 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
281 if (!hose->pci_prefetch ||
282 hose->pci_prefetch->size < hose->regions[i].size)
283 hose->pci_prefetch = hose->regions + i;
284 break;
wdenkc6097192002-11-03 00:24:07 +0000285 }
286 }
287
288
wdenk56ed43e2004-02-22 23:46:08 +0000289 if (hose->pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000290 pciauto_region_init(hose->pci_mem);
291
Ed Swarthouta5232962007-07-11 14:51:48 -0500292 DEBUGF("PCI Autoconfig: Bus Memory region: [%lx-%lx],\n"
293 "\t\tPhysical Memory [%x-%x]\n",
wdenkc6097192002-11-03 00:24:07 +0000294 hose->pci_mem->bus_start,
Ed Swarthouta5232962007-07-11 14:51:48 -0500295 hose->pci_mem->bus_start + hose->pci_mem->size - 1,
296 hose->pci_mem->phys_start,
297 hose->pci_mem->phys_start + hose->pci_mem->size - 1);
wdenkc6097192002-11-03 00:24:07 +0000298 }
299
Kumar Galae5ce4202006-01-11 13:24:15 -0600300 if (hose->pci_prefetch) {
301 pciauto_region_init(hose->pci_prefetch);
302
Ed Swarthouta5232962007-07-11 14:51:48 -0500303 DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [%lx-%lx],\n"
304 "\t\tPhysical Memory [%x-%x]\n",
Kumar Galae5ce4202006-01-11 13:24:15 -0600305 hose->pci_prefetch->bus_start,
Ed Swarthouta5232962007-07-11 14:51:48 -0500306 hose->pci_prefetch->bus_start + hose->pci_prefetch->size - 1,
307 hose->pci_prefetch->phys_start,
308 hose->pci_prefetch->phys_start +
309 hose->pci_prefetch->size - 1);
Kumar Galae5ce4202006-01-11 13:24:15 -0600310 }
311
wdenk56ed43e2004-02-22 23:46:08 +0000312 if (hose->pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000313 pciauto_region_init(hose->pci_io);
314
Ed Swarthouta5232962007-07-11 14:51:48 -0500315 DEBUGF("PCI Autoconfig: Bus I/O region: [%lx-%lx],\n"
316 "\t\tPhysical Memory: [%x-%x]\n",
wdenkc6097192002-11-03 00:24:07 +0000317 hose->pci_io->bus_start,
Ed Swarthouta5232962007-07-11 14:51:48 -0500318 hose->pci_io->bus_start + hose->pci_io->size - 1,
319 hose->pci_io->phys_start,
320 hose->pci_io->phys_start + hose->pci_io->size - 1);
321
wdenkc6097192002-11-03 00:24:07 +0000322 }
323}
324
wdenk452cfd62002-11-19 11:04:11 +0000325/* HJF: Changed this to return int. I think this is required
326 * to get the correct result when scanning bridges
327 */
328int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
wdenkc6097192002-11-03 00:24:07 +0000329{
wdenk452cfd62002-11-19 11:04:11 +0000330 unsigned int sub_bus = PCI_BUS(dev);
wdenkc6097192002-11-03 00:24:07 +0000331 unsigned short class;
332 unsigned char prg_iface;
wdenk2cefd152004-02-08 22:55:38 +0000333 int n;
wdenkc6097192002-11-03 00:24:07 +0000334
335 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
336
wdenk56ed43e2004-02-22 23:46:08 +0000337 switch(class) {
Ed Swarthout1d781f62007-07-11 14:52:16 -0500338 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
339 DEBUGF("PCI AutoConfig: Found PowerPC device\n");
340 pciauto_setup_device(hose, dev, 6, hose->pci_mem,
341 hose->pci_prefetch, hose->pci_io);
342 break;
343
wdenkc6097192002-11-03 00:24:07 +0000344 case PCI_CLASS_BRIDGE_PCI:
wdenkb666c8f2003-03-06 00:58:30 +0000345 hose->current_busno++;
Kumar Galae5ce4202006-01-11 13:24:15 -0600346 pciauto_setup_device(hose, dev, 2, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
wdenkc6097192002-11-03 00:24:07 +0000347
wdenkb666c8f2003-03-06 00:58:30 +0000348 DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev));
wdenk6cfa84e2004-02-10 00:03:41 +0000349
wdenk56ed43e2004-02-22 23:46:08 +0000350 /* Passing in current_busno allows for sibling P2P bridges */
wdenk2cefd152004-02-08 22:55:38 +0000351 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
wdenk6cfa84e2004-02-10 00:03:41 +0000352 /*
wdenk56ed43e2004-02-22 23:46:08 +0000353 * need to figure out if this is a subordinate bridge on the bus
wdenk2cefd152004-02-08 22:55:38 +0000354 * to be able to properly set the pri/sec/sub bridge registers.
355 */
356 n = pci_hose_scan_bus(hose, hose->current_busno);
wdenk57b2d802003-06-27 21:31:46 +0000357
wdenk56ed43e2004-02-22 23:46:08 +0000358 /* figure out the deepest we've gone for this leg */
wdenk2cefd152004-02-08 22:55:38 +0000359 sub_bus = max(n, sub_bus);
wdenkb666c8f2003-03-06 00:58:30 +0000360 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
wdenk2cefd152004-02-08 22:55:38 +0000361
wdenkb666c8f2003-03-06 00:58:30 +0000362 sub_bus = hose->current_busno;
wdenkc6097192002-11-03 00:24:07 +0000363 break;
364
365 case PCI_CLASS_STORAGE_IDE:
366 pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface);
wdenk56ed43e2004-02-22 23:46:08 +0000367 if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
368 DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
369 return sub_bus;
370 }
wdenkc6097192002-11-03 00:24:07 +0000371
Kumar Galae5ce4202006-01-11 13:24:15 -0600372 pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
wdenkc6097192002-11-03 00:24:07 +0000373 break;
374
wdenk1fe2c702003-03-06 21:55:29 +0000375 case PCI_CLASS_BRIDGE_CARDBUS:
376 /* just do a minimal setup of the bridge, let the OS take care of the rest */
Kumar Galae5ce4202006-01-11 13:24:15 -0600377 pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
wdenk1fe2c702003-03-06 21:55:29 +0000378
wdenk56ed43e2004-02-22 23:46:08 +0000379 DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n", PCI_DEV(dev));
wdenk1fe2c702003-03-06 21:55:29 +0000380
381 hose->current_busno++;
382 break;
383
TsiChung Liew521f97b2008-03-30 01:19:06 -0500384#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
wdenk5d841732003-08-17 18:55:18 +0000385 case PCI_CLASS_BRIDGE_OTHER:
386 DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
387 PCI_DEV(dev));
388 break;
389#endif
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200390#ifdef CONFIG_MPC834X
391 case PCI_CLASS_BRIDGE_OTHER:
392 /*
393 * The host/PCI bridge 1 seems broken in 8349 - it presents
394 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
395 * device claiming resources io/mem/irq.. we only allow for
396 * the PIMMR window to be allocated (BAR0 - 1MB size)
397 */
398 DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
Kumar Galae5ce4202006-01-11 13:24:15 -0600399 pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200400 break;
401#endif
wdenkc6097192002-11-03 00:24:07 +0000402 default:
Kumar Galae5ce4202006-01-11 13:24:15 -0600403 pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_prefetch, hose->pci_io);
wdenkc6097192002-11-03 00:24:07 +0000404 break;
405 }
wdenk452cfd62002-11-19 11:04:11 +0000406
407 return sub_bus;
wdenkc6097192002-11-03 00:24:07 +0000408}