blob: e034ed1715cfcda988312bbb2dcce6cbea3177e2 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Stefan Roese88fbf932010-04-15 16:07:28 +02002 * arch/powerpc/kernel/pci_auto.c
wdenkc6097192002-11-03 00:24:07 +00003 *
4 * PCI autoconfiguration library
5 *
6 * Author: Matt Porter <mporter@mvista.com>
7 *
8 * Copyright 2000 MontaVista Software Inc.
9 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +000011 */
12
13#include <common.h>
Simon Glass1c1695b2015-01-14 21:37:04 -070014#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <pci.h>
16
wdenkc6097192002-11-03 00:24:07 +000017#ifdef DEBUG
18#define DEBUGF(x...) printf(x)
19#else
20#define DEBUGF(x...)
21#endif /* DEBUG */
22
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023/* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
24#ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
25#define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
Gary Jennejohn9a1263f2007-08-31 15:21:46 +020026#endif
27
wdenkc6097192002-11-03 00:24:07 +000028/*
29 *
30 */
31
Andrew Sharp68705132012-08-29 14:16:29 +000032void pciauto_region_init(struct pci_region *res)
wdenkc6097192002-11-03 00:24:07 +000033{
Sergei Shtylyov9679f4d2007-04-23 15:30:39 +020034 /*
35 * Avoid allocating PCI resources from address 0 -- this is illegal
36 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
37 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
38 */
39 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
wdenkc6097192002-11-03 00:24:07 +000040}
41
Kumar Galaad714f52008-10-21 08:36:08 -050042void pciauto_region_align(struct pci_region *res, pci_size_t size)
wdenkc6097192002-11-03 00:24:07 +000043{
44 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
45}
46
Andrew Sharp68705132012-08-29 14:16:29 +000047int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
48 pci_addr_t *bar)
wdenkc6097192002-11-03 00:24:07 +000049{
Kumar Galaad714f52008-10-21 08:36:08 -050050 pci_addr_t addr;
wdenkc6097192002-11-03 00:24:07 +000051
wdenk56ed43e2004-02-22 23:46:08 +000052 if (!res) {
wdenkc6097192002-11-03 00:24:07 +000053 DEBUGF("No resource");
54 goto error;
55 }
56
57 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
58
wdenk56ed43e2004-02-22 23:46:08 +000059 if (addr - res->bus_start + size > res->size) {
wdenkc6097192002-11-03 00:24:07 +000060 DEBUGF("No room in resource");
61 goto error;
62 }
63
64 res->bus_lower = addr + size;
65
Kumar Galaad714f52008-10-21 08:36:08 -050066 DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower);
wdenkc6097192002-11-03 00:24:07 +000067
68 *bar = addr;
69 return 0;
70
71 error:
Kumar Galaad714f52008-10-21 08:36:08 -050072 *bar = (pci_addr_t)-1;
wdenkc6097192002-11-03 00:24:07 +000073 return -1;
74}
75
76/*
77 *
78 */
79
80void pciauto_setup_device(struct pci_controller *hose,
81 pci_dev_t dev, int bars_num,
82 struct pci_region *mem,
Kumar Galae5ce4202006-01-11 13:24:15 -060083 struct pci_region *prefetch,
wdenkc6097192002-11-03 00:24:07 +000084 struct pci_region *io)
85{
Kumar Gala1873d5c2012-09-19 04:47:36 +000086 u32 bar_response;
Kumar Galaad714f52008-10-21 08:36:08 -050087 pci_size_t bar_size;
Andrew Sharpf4f24822012-08-01 12:27:16 +000088 u16 cmdstat = 0;
wdenkc6097192002-11-03 00:24:07 +000089 int bar, bar_nr = 0;
Bin Meng51e98ca2015-07-08 13:06:40 +080090 u8 header_type;
91 int rom_addr;
Andrew Sharp61d47ca2012-08-29 14:16:32 +000092#ifndef CONFIG_PCI_ENUM_ONLY
93 pci_addr_t bar_value;
94 struct pci_region *bar_res;
wdenkc6097192002-11-03 00:24:07 +000095 int found_mem64 = 0;
Andrew Sharp61d47ca2012-08-29 14:16:32 +000096#endif
wdenkc6097192002-11-03 00:24:07 +000097
Andrew Sharpf4f24822012-08-01 12:27:16 +000098 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
wdenkc6097192002-11-03 00:24:07 +000099 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
100
Andrew Sharp68705132012-08-29 14:16:29 +0000101 for (bar = PCI_BASE_ADDRESS_0;
102 bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
wdenkc6097192002-11-03 00:24:07 +0000103 /* Tickle the BAR and get the response */
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000104#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000105 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000106#endif
wdenkc6097192002-11-03 00:24:07 +0000107 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
108
109 /* If BAR is not implemented go to the next BAR */
110 if (!bar_response)
111 continue;
112
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000113#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000114 found_mem64 = 0;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000115#endif
wdenkc6097192002-11-03 00:24:07 +0000116
117 /* Check the BAR type and set our address mask */
wdenk56ed43e2004-02-22 23:46:08 +0000118 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Jin Zhengxiong-R64188f4ff3e82006-06-27 18:12:02 +0800119 bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
120 & 0xffff) + 1;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000121#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000122 bar_res = io;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000123#endif
wdenkc6097192002-11-03 00:24:07 +0000124
Kumar Galaad714f52008-10-21 08:36:08 -0500125 DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ", bar_nr, (u64)bar_size);
wdenk56ed43e2004-02-22 23:46:08 +0000126 } else {
Andrew Sharp68705132012-08-29 14:16:29 +0000127 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
Kumar Galaad714f52008-10-21 08:36:08 -0500128 PCI_BASE_ADDRESS_MEM_TYPE_64) {
129 u32 bar_response_upper;
130 u64 bar64;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000131
132#ifndef CONFIG_PCI_ENUM_ONLY
Andrew Sharp68705132012-08-29 14:16:29 +0000133 pci_hose_write_config_dword(hose, dev, bar + 4,
134 0xffffffff);
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000135#endif
Andrew Sharp68705132012-08-29 14:16:29 +0000136 pci_hose_read_config_dword(hose, dev, bar + 4,
137 &bar_response_upper);
Kumar Galaad714f52008-10-21 08:36:08 -0500138
139 bar64 = ((u64)bar_response_upper << 32) | bar_response;
wdenkc6097192002-11-03 00:24:07 +0000140
Kumar Galaad714f52008-10-21 08:36:08 -0500141 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000142#ifndef CONFIG_PCI_ENUM_ONLY
Kumar Galaad714f52008-10-21 08:36:08 -0500143 found_mem64 = 1;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000144#endif
Kumar Galaad714f52008-10-21 08:36:08 -0500145 } else {
146 bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
147 }
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000148#ifndef CONFIG_PCI_ENUM_ONLY
Kumar Galae5ce4202006-01-11 13:24:15 -0600149 if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
150 bar_res = prefetch;
151 else
152 bar_res = mem;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000153#endif
wdenkc6097192002-11-03 00:24:07 +0000154
Kumar Galaad714f52008-10-21 08:36:08 -0500155 DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%llx, ", bar_nr, (u64)bar_size);
wdenkc6097192002-11-03 00:24:07 +0000156 }
157
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000158#ifndef CONFIG_PCI_ENUM_ONLY
wdenk56ed43e2004-02-22 23:46:08 +0000159 if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000160 /* Write it out and update our limit */
Kumar Galaad714f52008-10-21 08:36:08 -0500161 pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value);
wdenkc6097192002-11-03 00:24:07 +0000162
wdenk56ed43e2004-02-22 23:46:08 +0000163 if (found_mem64) {
wdenkc6097192002-11-03 00:24:07 +0000164 bar += 4;
Kumar Galaad714f52008-10-21 08:36:08 -0500165#ifdef CONFIG_SYS_PCI_64BIT
166 pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
167#else
168 /*
169 * If we are a 64-bit decoder then increment to the
170 * upper 32 bits of the bar and force it to locate
171 * in the lower 4GB of memory.
172 */
wdenkc6097192002-11-03 00:24:07 +0000173 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
Kumar Galaad714f52008-10-21 08:36:08 -0500174#endif
wdenkc6097192002-11-03 00:24:07 +0000175 }
176
wdenkc6097192002-11-03 00:24:07 +0000177 }
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000178#endif
179 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
180 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
wdenkc6097192002-11-03 00:24:07 +0000181
182 DEBUGF("\n");
183
184 bar_nr++;
185 }
186
Bin Meng51e98ca2015-07-08 13:06:40 +0800187 /* Configure the expansion ROM address */
188 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
189 if (header_type != PCI_HEADER_TYPE_CARDBUS) {
190 rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
191 PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
192 pci_hose_write_config_dword(hose, dev, rom_addr, 0xfffffffe);
193 pci_hose_read_config_dword(hose, dev, rom_addr, &bar_response);
194 if (bar_response) {
195 bar_size = -(bar_response & ~1);
196 DEBUGF("PCI Autoconfig: ROM, size=%#x, ", bar_size);
197 if (pciauto_region_allocate(mem, bar_size,
198 &bar_value) == 0) {
199 pci_hose_write_config_dword(hose, dev, rom_addr,
200 bar_value);
201 }
202 cmdstat |= PCI_COMMAND_MEMORY;
203 DEBUGF("\n");
204 }
205 }
206
Andrew Sharpf4f24822012-08-01 12:27:16 +0000207 pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
Gary Jennejohn9a1263f2007-08-31 15:21:46 +0200208 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200209 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000210 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
211}
212
Ed Swarthouta5232962007-07-11 14:51:48 -0500213void pciauto_prescan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000214 pci_dev_t dev, int sub_bus)
215{
216 struct pci_region *pci_mem = hose->pci_mem;
Kumar Galae5ce4202006-01-11 13:24:15 -0600217 struct pci_region *pci_prefetch = hose->pci_prefetch;
wdenkc6097192002-11-03 00:24:07 +0000218 struct pci_region *pci_io = hose->pci_io;
David Feng3be54fd2015-02-02 16:53:13 +0800219 u16 cmdstat, prefechable_64;
wdenkc6097192002-11-03 00:24:07 +0000220
Andrew Sharpf4f24822012-08-01 12:27:16 +0000221 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
David Feng3be54fd2015-02-02 16:53:13 +0800222 pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
223 &prefechable_64);
224 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
wdenkc6097192002-11-03 00:24:07 +0000225
226 /* Configure bus number registers */
Ed Swarthout4aeb55a2007-07-11 14:52:08 -0500227 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
228 PCI_BUS(dev) - hose->first_busno);
229 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
230 sub_bus - hose->first_busno);
wdenkc6097192002-11-03 00:24:07 +0000231 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
232
wdenk56ed43e2004-02-22 23:46:08 +0000233 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000234 /* Round memory allocator to 1MB boundary */
235 pciauto_region_align(pci_mem, 0x100000);
236
237 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
238 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
239 (pci_mem->bus_lower & 0xfff00000) >> 16);
240
241 cmdstat |= PCI_COMMAND_MEMORY;
242 }
243
Kumar Galae5ce4202006-01-11 13:24:15 -0600244 if (pci_prefetch) {
245 /* Round memory allocator to 1MB boundary */
246 pciauto_region_align(pci_prefetch, 0x100000);
247
248 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
249 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
250 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
David Feng3be54fd2015-02-02 16:53:13 +0800251 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
252#ifdef CONFIG_SYS_PCI_64BIT
253 pci_hose_write_config_dword(hose, dev,
254 PCI_PREF_BASE_UPPER32,
255 pci_prefetch->bus_lower >> 32);
256#else
257 pci_hose_write_config_dword(hose, dev,
258 PCI_PREF_BASE_UPPER32,
259 0x0);
260#endif
Kumar Galae5ce4202006-01-11 13:24:15 -0600261
262 cmdstat |= PCI_COMMAND_MEMORY;
263 } else {
264 /* We don't support prefetchable memory for now, so disable */
265 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
Matthew McClintock2f43f332006-06-28 10:44:23 -0500266 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
David Feng3be54fd2015-02-02 16:53:13 +0800267 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
268 pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
269 pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
270 }
Kumar Galae5ce4202006-01-11 13:24:15 -0600271 }
272
wdenk56ed43e2004-02-22 23:46:08 +0000273 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000274 /* Round I/O allocator to 4KB boundary */
275 pciauto_region_align(pci_io, 0x1000);
276
277 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
278 (pci_io->bus_lower & 0x0000f000) >> 8);
279 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
280 (pci_io->bus_lower & 0xffff0000) >> 16);
281
282 cmdstat |= PCI_COMMAND_IO;
283 }
284
wdenkc6097192002-11-03 00:24:07 +0000285 /* Enable memory and I/O accesses, enable bus master */
Andrew Sharpf4f24822012-08-01 12:27:16 +0000286 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
287 cmdstat | PCI_COMMAND_MASTER);
wdenkc6097192002-11-03 00:24:07 +0000288}
289
Ed Swarthouta5232962007-07-11 14:51:48 -0500290void pciauto_postscan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000291 pci_dev_t dev, int sub_bus)
292{
293 struct pci_region *pci_mem = hose->pci_mem;
Kumar Galae5ce4202006-01-11 13:24:15 -0600294 struct pci_region *pci_prefetch = hose->pci_prefetch;
wdenkc6097192002-11-03 00:24:07 +0000295 struct pci_region *pci_io = hose->pci_io;
296
297 /* Configure bus number registers */
Ed Swarthout4aeb55a2007-07-11 14:52:08 -0500298 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
299 sub_bus - hose->first_busno);
wdenkc6097192002-11-03 00:24:07 +0000300
wdenk56ed43e2004-02-22 23:46:08 +0000301 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000302 /* Round memory allocator to 1MB boundary */
303 pciauto_region_align(pci_mem, 0x100000);
304
305 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
Andrew Sharp68705132012-08-29 14:16:29 +0000306 (pci_mem->bus_lower - 1) >> 16);
wdenkc6097192002-11-03 00:24:07 +0000307 }
308
Kumar Galae5ce4202006-01-11 13:24:15 -0600309 if (pci_prefetch) {
David Feng3be54fd2015-02-02 16:53:13 +0800310 u16 prefechable_64;
311
312 pci_hose_read_config_word(hose, dev,
313 PCI_PREF_MEMORY_LIMIT,
314 &prefechable_64);
315 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
316
Kumar Galae5ce4202006-01-11 13:24:15 -0600317 /* Round memory allocator to 1MB boundary */
318 pciauto_region_align(pci_prefetch, 0x100000);
319
320 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
Andrew Sharp68705132012-08-29 14:16:29 +0000321 (pci_prefetch->bus_lower - 1) >> 16);
David Feng3be54fd2015-02-02 16:53:13 +0800322 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
323#ifdef CONFIG_SYS_PCI_64BIT
324 pci_hose_write_config_dword(hose, dev,
325 PCI_PREF_LIMIT_UPPER32,
326 (pci_prefetch->bus_lower - 1) >> 32);
327#else
328 pci_hose_write_config_dword(hose, dev,
329 PCI_PREF_LIMIT_UPPER32,
330 0x0);
331#endif
Kumar Galae5ce4202006-01-11 13:24:15 -0600332 }
333
wdenk56ed43e2004-02-22 23:46:08 +0000334 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000335 /* Round I/O allocator to 4KB boundary */
336 pciauto_region_align(pci_io, 0x1000);
337
338 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
Andrew Sharp68705132012-08-29 14:16:29 +0000339 ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
wdenkc6097192002-11-03 00:24:07 +0000340 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
Andrew Sharp68705132012-08-29 14:16:29 +0000341 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
wdenkc6097192002-11-03 00:24:07 +0000342 }
343}
344
345/*
346 *
347 */
348
349void pciauto_config_init(struct pci_controller *hose)
350{
351 int i;
352
Thierry Redinga3d5df32013-09-20 15:50:50 +0200353 hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
wdenkc6097192002-11-03 00:24:07 +0000354
Andrew Sharp68705132012-08-29 14:16:29 +0000355 for (i = 0; i < hose->region_count; i++) {
wdenk56ed43e2004-02-22 23:46:08 +0000356 switch(hose->regions[i].flags) {
wdenkc6097192002-11-03 00:24:07 +0000357 case PCI_REGION_IO:
358 if (!hose->pci_io ||
359 hose->pci_io->size < hose->regions[i].size)
360 hose->pci_io = hose->regions + i;
361 break;
362 case PCI_REGION_MEM:
363 if (!hose->pci_mem ||
364 hose->pci_mem->size < hose->regions[i].size)
365 hose->pci_mem = hose->regions + i;
366 break;
Kumar Galae5ce4202006-01-11 13:24:15 -0600367 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
368 if (!hose->pci_prefetch ||
369 hose->pci_prefetch->size < hose->regions[i].size)
370 hose->pci_prefetch = hose->regions + i;
371 break;
wdenkc6097192002-11-03 00:24:07 +0000372 }
373 }
374
375
wdenk56ed43e2004-02-22 23:46:08 +0000376 if (hose->pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000377 pciauto_region_init(hose->pci_mem);
378
Kumar Galaad714f52008-10-21 08:36:08 -0500379 DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
380 "\t\tPhysical Memory [%llx-%llxx]\n",
381 (u64)hose->pci_mem->bus_start,
382 (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
383 (u64)hose->pci_mem->phys_start,
384 (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
wdenkc6097192002-11-03 00:24:07 +0000385 }
386
Kumar Galae5ce4202006-01-11 13:24:15 -0600387 if (hose->pci_prefetch) {
388 pciauto_region_init(hose->pci_prefetch);
389
Kumar Galaad714f52008-10-21 08:36:08 -0500390 DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
391 "\t\tPhysical Memory [%llx-%llx]\n",
392 (u64)hose->pci_prefetch->bus_start,
393 (u64)(hose->pci_prefetch->bus_start +
394 hose->pci_prefetch->size - 1),
395 (u64)hose->pci_prefetch->phys_start,
396 (u64)(hose->pci_prefetch->phys_start +
397 hose->pci_prefetch->size - 1));
Kumar Galae5ce4202006-01-11 13:24:15 -0600398 }
399
wdenk56ed43e2004-02-22 23:46:08 +0000400 if (hose->pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000401 pciauto_region_init(hose->pci_io);
402
Kumar Galaad714f52008-10-21 08:36:08 -0500403 DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
404 "\t\tPhysical Memory: [%llx-%llx]\n",
405 (u64)hose->pci_io->bus_start,
406 (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
407 (u64)hose->pci_io->phys_start,
408 (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
Ed Swarthouta5232962007-07-11 14:51:48 -0500409
wdenkc6097192002-11-03 00:24:07 +0000410 }
411}
412
Andrew Sharp68705132012-08-29 14:16:29 +0000413/*
414 * HJF: Changed this to return int. I think this is required
wdenk452cfd62002-11-19 11:04:11 +0000415 * to get the correct result when scanning bridges
416 */
417int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
wdenkc6097192002-11-03 00:24:07 +0000418{
wdenk452cfd62002-11-19 11:04:11 +0000419 unsigned int sub_bus = PCI_BUS(dev);
wdenkc6097192002-11-03 00:24:07 +0000420 unsigned short class;
wdenk2cefd152004-02-08 22:55:38 +0000421 int n;
wdenkc6097192002-11-03 00:24:07 +0000422
423 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
424
Andrew Sharp68705132012-08-29 14:16:29 +0000425 switch (class) {
wdenkc6097192002-11-03 00:24:07 +0000426 case PCI_CLASS_BRIDGE_PCI:
Simon Glassb94dc892015-03-05 12:25:25 -0700427 DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n",
428 PCI_DEV(dev));
429
Andrew Sharp68705132012-08-29 14:16:29 +0000430 pciauto_setup_device(hose, dev, 2, hose->pci_mem,
431 hose->pci_prefetch, hose->pci_io);
wdenkc6097192002-11-03 00:24:07 +0000432
Simon Glassb94dc892015-03-05 12:25:25 -0700433#ifdef CONFIG_DM_PCI
434 n = dm_pci_hose_probe_bus(hose, dev);
435 if (n < 0)
436 return n;
437 sub_bus = (unsigned int)n;
438#else
wdenk56ed43e2004-02-22 23:46:08 +0000439 /* Passing in current_busno allows for sibling P2P bridges */
Simon Glassb94dc892015-03-05 12:25:25 -0700440 hose->current_busno++;
wdenk2cefd152004-02-08 22:55:38 +0000441 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
wdenk6cfa84e2004-02-10 00:03:41 +0000442 /*
wdenk56ed43e2004-02-22 23:46:08 +0000443 * need to figure out if this is a subordinate bridge on the bus
wdenk2cefd152004-02-08 22:55:38 +0000444 * to be able to properly set the pri/sec/sub bridge registers.
445 */
446 n = pci_hose_scan_bus(hose, hose->current_busno);
wdenk57b2d802003-06-27 21:31:46 +0000447
wdenk56ed43e2004-02-22 23:46:08 +0000448 /* figure out the deepest we've gone for this leg */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900449 sub_bus = max((unsigned int)n, sub_bus);
wdenkb666c8f2003-03-06 00:58:30 +0000450 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
wdenk2cefd152004-02-08 22:55:38 +0000451
wdenkb666c8f2003-03-06 00:58:30 +0000452 sub_bus = hose->current_busno;
Simon Glassb94dc892015-03-05 12:25:25 -0700453#endif
wdenkc6097192002-11-03 00:24:07 +0000454 break;
455
wdenk1fe2c702003-03-06 21:55:29 +0000456 case PCI_CLASS_BRIDGE_CARDBUS:
Andrew Sharp68705132012-08-29 14:16:29 +0000457 /*
458 * just do a minimal setup of the bridge,
459 * let the OS take care of the rest
460 */
461 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
462 hose->pci_prefetch, hose->pci_io);
wdenk1fe2c702003-03-06 21:55:29 +0000463
Andrew Sharp68705132012-08-29 14:16:29 +0000464 DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
465 PCI_DEV(dev));
wdenk1fe2c702003-03-06 21:55:29 +0000466
Simon Glassb94dc892015-03-05 12:25:25 -0700467#ifndef CONFIG_DM_PCI
wdenk1fe2c702003-03-06 21:55:29 +0000468 hose->current_busno++;
Simon Glassb94dc892015-03-05 12:25:25 -0700469#endif
wdenk1fe2c702003-03-06 21:55:29 +0000470 break;
471
TsiChung Liew521f97b2008-03-30 01:19:06 -0500472#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
wdenk5d841732003-08-17 18:55:18 +0000473 case PCI_CLASS_BRIDGE_OTHER:
474 DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
475 PCI_DEV(dev));
476 break;
477#endif
Reinhard Arlt46911792009-07-25 06:19:12 +0200478#if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200479 case PCI_CLASS_BRIDGE_OTHER:
480 /*
481 * The host/PCI bridge 1 seems broken in 8349 - it presents
482 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
483 * device claiming resources io/mem/irq.. we only allow for
484 * the PIMMR window to be allocated (BAR0 - 1MB size)
485 */
486 DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
Andrew Sharp68705132012-08-29 14:16:29 +0000487 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
488 hose->pci_prefetch, hose->pci_io);
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200489 break;
490#endif
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000491
492 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
493 DEBUGF("PCI AutoConfig: Found PowerPC device\n");
494
wdenkc6097192002-11-03 00:24:07 +0000495 default:
Andrew Sharp68705132012-08-29 14:16:29 +0000496 pciauto_setup_device(hose, dev, 6, hose->pci_mem,
497 hose->pci_prefetch, hose->pci_io);
wdenkc6097192002-11-03 00:24:07 +0000498 break;
499 }
wdenk452cfd62002-11-19 11:04:11 +0000500
501 return sub_bus;
wdenkc6097192002-11-03 00:24:07 +0000502}