blob: 7ff282b48cb07c8a8bf9d7b7937fe3c5527d4590 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Stefan Roesea47a12b2010-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 Denk1a459662013-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 Glass4a2708a2015-01-14 21:37:04 -070014#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <pci.h>
16
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020017/* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
18#ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
19#define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
Gary Jennejohn81b73de2007-08-31 15:21:46 +020020#endif
21
wdenkc6097192002-11-03 00:24:07 +000022/*
23 *
24 */
25
Andrew Sharpcb2bf932012-08-29 14:16:29 +000026void pciauto_region_init(struct pci_region *res)
wdenkc6097192002-11-03 00:24:07 +000027{
Sergei Shtylyovb7598a42007-04-23 15:30:39 +020028 /*
29 * Avoid allocating PCI resources from address 0 -- this is illegal
30 * according to PCI 2.1 and moreover, this is known to cause Linux IDE
31 * drivers to fail. Use a reasonable starting value of 0x1000 instead.
32 */
33 res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
wdenkc6097192002-11-03 00:24:07 +000034}
35
Kumar Gala30e76d52008-10-21 08:36:08 -050036void pciauto_region_align(struct pci_region *res, pci_size_t size)
wdenkc6097192002-11-03 00:24:07 +000037{
38 res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
39}
40
Andrew Sharpcb2bf932012-08-29 14:16:29 +000041int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
42 pci_addr_t *bar)
wdenkc6097192002-11-03 00:24:07 +000043{
Kumar Gala30e76d52008-10-21 08:36:08 -050044 pci_addr_t addr;
wdenkc6097192002-11-03 00:24:07 +000045
wdenk3c74e322004-02-22 23:46:08 +000046 if (!res) {
Simon Glassda4b1592015-07-31 09:31:33 -060047 debug("No resource");
wdenkc6097192002-11-03 00:24:07 +000048 goto error;
49 }
50
51 addr = ((res->bus_lower - 1) | (size - 1)) + 1;
52
wdenk3c74e322004-02-22 23:46:08 +000053 if (addr - res->bus_start + size > res->size) {
Simon Glassda4b1592015-07-31 09:31:33 -060054 debug("No room in resource");
wdenkc6097192002-11-03 00:24:07 +000055 goto error;
56 }
57
58 res->bus_lower = addr + size;
59
Simon Glassda4b1592015-07-31 09:31:33 -060060 debug("address=0x%llx bus_lower=0x%llx", (unsigned long long)addr,
61 (unsigned long long)res->bus_lower);
wdenkc6097192002-11-03 00:24:07 +000062
63 *bar = addr;
64 return 0;
65
66 error:
Kumar Gala30e76d52008-10-21 08:36:08 -050067 *bar = (pci_addr_t)-1;
wdenkc6097192002-11-03 00:24:07 +000068 return -1;
69}
70
71/*
72 *
73 */
74
75void pciauto_setup_device(struct pci_controller *hose,
76 pci_dev_t dev, int bars_num,
77 struct pci_region *mem,
Kumar Galaa1790122006-01-11 13:24:15 -060078 struct pci_region *prefetch,
wdenkc6097192002-11-03 00:24:07 +000079 struct pci_region *io)
80{
Kumar Galacf5787f2012-09-19 04:47:36 +000081 u32 bar_response;
Kumar Gala30e76d52008-10-21 08:36:08 -050082 pci_size_t bar_size;
Andrew Sharpaf778c62012-08-01 12:27:16 +000083 u16 cmdstat = 0;
wdenkc6097192002-11-03 00:24:07 +000084 int bar, bar_nr = 0;
Bin Meng6c896632015-07-08 13:06:40 +080085 u8 header_type;
86 int rom_addr;
Andrew Sharp69fd2d32012-08-29 14:16:32 +000087#ifndef CONFIG_PCI_ENUM_ONLY
88 pci_addr_t bar_value;
89 struct pci_region *bar_res;
wdenkc6097192002-11-03 00:24:07 +000090 int found_mem64 = 0;
Andrew Sharp69fd2d32012-08-29 14:16:32 +000091#endif
wdenkc6097192002-11-03 00:24:07 +000092
Andrew Sharpaf778c62012-08-01 12:27:16 +000093 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
wdenkc6097192002-11-03 00:24:07 +000094 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
95
Andrew Sharpcb2bf932012-08-29 14:16:29 +000096 for (bar = PCI_BASE_ADDRESS_0;
97 bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
wdenkc6097192002-11-03 00:24:07 +000098 /* Tickle the BAR and get the response */
Andrew Sharp69fd2d32012-08-29 14:16:32 +000099#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000100 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000101#endif
wdenkc6097192002-11-03 00:24:07 +0000102 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
103
104 /* If BAR is not implemented go to the next BAR */
105 if (!bar_response)
106 continue;
107
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000108#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000109 found_mem64 = 0;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000110#endif
wdenkc6097192002-11-03 00:24:07 +0000111
112 /* Check the BAR type and set our address mask */
wdenk3c74e322004-02-22 23:46:08 +0000113 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Jin Zhengxiong-R64188bd22c2b2006-06-27 18:12:02 +0800114 bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
115 & 0xffff) + 1;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000116#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000117 bar_res = io;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000118#endif
wdenkc6097192002-11-03 00:24:07 +0000119
Simon Glassda4b1592015-07-31 09:31:33 -0600120 debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ",
121 bar_nr, (unsigned long long)bar_size);
wdenk3c74e322004-02-22 23:46:08 +0000122 } else {
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000123 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
Kumar Gala30e76d52008-10-21 08:36:08 -0500124 PCI_BASE_ADDRESS_MEM_TYPE_64) {
125 u32 bar_response_upper;
126 u64 bar64;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000127
128#ifndef CONFIG_PCI_ENUM_ONLY
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000129 pci_hose_write_config_dword(hose, dev, bar + 4,
130 0xffffffff);
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000131#endif
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000132 pci_hose_read_config_dword(hose, dev, bar + 4,
133 &bar_response_upper);
wdenkc6097192002-11-03 00:24:07 +0000134
Kumar Gala30e76d52008-10-21 08:36:08 -0500135 bar64 = ((u64)bar_response_upper << 32) | bar_response;
136
137 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000138#ifndef CONFIG_PCI_ENUM_ONLY
Kumar Gala30e76d52008-10-21 08:36:08 -0500139 found_mem64 = 1;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000140#endif
Kumar Gala30e76d52008-10-21 08:36:08 -0500141 } else {
142 bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
143 }
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000144#ifndef CONFIG_PCI_ENUM_ONLY
Kumar Galaa1790122006-01-11 13:24:15 -0600145 if (prefetch && (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
146 bar_res = prefetch;
147 else
148 bar_res = mem;
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000149#endif
wdenkc6097192002-11-03 00:24:07 +0000150
Simon Glassda4b1592015-07-31 09:31:33 -0600151 debug("PCI Autoconfig: BAR %d, Mem, size=0x%llx, ",
152 bar_nr, (unsigned long long)bar_size);
wdenkc6097192002-11-03 00:24:07 +0000153 }
154
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000155#ifndef CONFIG_PCI_ENUM_ONLY
wdenk3c74e322004-02-22 23:46:08 +0000156 if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000157 /* Write it out and update our limit */
Kumar Gala30e76d52008-10-21 08:36:08 -0500158 pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value);
wdenkc6097192002-11-03 00:24:07 +0000159
wdenk3c74e322004-02-22 23:46:08 +0000160 if (found_mem64) {
wdenkc6097192002-11-03 00:24:07 +0000161 bar += 4;
Kumar Gala30e76d52008-10-21 08:36:08 -0500162#ifdef CONFIG_SYS_PCI_64BIT
163 pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
164#else
165 /*
166 * If we are a 64-bit decoder then increment to the
167 * upper 32 bits of the bar and force it to locate
168 * in the lower 4GB of memory.
169 */
wdenkc6097192002-11-03 00:24:07 +0000170 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
Kumar Gala30e76d52008-10-21 08:36:08 -0500171#endif
wdenkc6097192002-11-03 00:24:07 +0000172 }
173
wdenkc6097192002-11-03 00:24:07 +0000174 }
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000175#endif
176 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
177 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
wdenkc6097192002-11-03 00:24:07 +0000178
Simon Glassda4b1592015-07-31 09:31:33 -0600179 debug("\n");
wdenkc6097192002-11-03 00:24:07 +0000180
181 bar_nr++;
182 }
183
Bin Meng6c896632015-07-08 13:06:40 +0800184 /* Configure the expansion ROM address */
185 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
186 if (header_type != PCI_HEADER_TYPE_CARDBUS) {
187 rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
188 PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
189 pci_hose_write_config_dword(hose, dev, rom_addr, 0xfffffffe);
190 pci_hose_read_config_dword(hose, dev, rom_addr, &bar_response);
191 if (bar_response) {
192 bar_size = -(bar_response & ~1);
Simon Glassda4b1592015-07-31 09:31:33 -0600193 debug("PCI Autoconfig: ROM, size=%#x, ",
194 (unsigned int)bar_size);
Bin Meng6c896632015-07-08 13:06:40 +0800195 if (pciauto_region_allocate(mem, bar_size,
196 &bar_value) == 0) {
197 pci_hose_write_config_dword(hose, dev, rom_addr,
198 bar_value);
199 }
200 cmdstat |= PCI_COMMAND_MEMORY;
Simon Glassda4b1592015-07-31 09:31:33 -0600201 debug("\n");
Bin Meng6c896632015-07-08 13:06:40 +0800202 }
203 }
204
Andrew Sharpaf778c62012-08-01 12:27:16 +0000205 pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
Gary Jennejohn81b73de2007-08-31 15:21:46 +0200206 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200207 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000208 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
209}
210
Ed Swarthoutba5feb12007-07-11 14:51:48 -0500211void pciauto_prescan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000212 pci_dev_t dev, int sub_bus)
213{
Bin Mengd11d9ef2015-07-19 00:20:06 +0800214 struct pci_region *pci_mem;
215 struct pci_region *pci_prefetch;
216 struct pci_region *pci_io;
David Feng6eefd522015-02-02 16:53:13 +0800217 u16 cmdstat, prefechable_64;
wdenkc6097192002-11-03 00:24:07 +0000218
Bin Mengd11d9ef2015-07-19 00:20:06 +0800219#ifdef CONFIG_DM_PCI
220 /* The root controller has the region information */
221 struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
222
223 pci_mem = ctlr_hose->pci_mem;
224 pci_prefetch = ctlr_hose->pci_prefetch;
225 pci_io = ctlr_hose->pci_io;
226#else
227 pci_mem = hose->pci_mem;
228 pci_prefetch = hose->pci_prefetch;
229 pci_io = hose->pci_io;
230#endif
231
Andrew Sharpaf778c62012-08-01 12:27:16 +0000232 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
David Feng6eefd522015-02-02 16:53:13 +0800233 pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
234 &prefechable_64);
235 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
wdenkc6097192002-11-03 00:24:07 +0000236
237 /* Configure bus number registers */
Bin Meng95f3aa22015-07-19 00:20:03 +0800238#ifdef CONFIG_DM_PCI
239 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, PCI_BUS(dev));
240 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, sub_bus);
241#else
Ed Swarthoute8b85f32007-07-11 14:52:08 -0500242 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
243 PCI_BUS(dev) - hose->first_busno);
244 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
245 sub_bus - hose->first_busno);
Bin Meng95f3aa22015-07-19 00:20:03 +0800246#endif
wdenkc6097192002-11-03 00:24:07 +0000247 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
248
wdenk3c74e322004-02-22 23:46:08 +0000249 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000250 /* Round memory allocator to 1MB boundary */
251 pciauto_region_align(pci_mem, 0x100000);
252
253 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
254 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
255 (pci_mem->bus_lower & 0xfff00000) >> 16);
256
257 cmdstat |= PCI_COMMAND_MEMORY;
258 }
259
Kumar Galaa1790122006-01-11 13:24:15 -0600260 if (pci_prefetch) {
261 /* Round memory allocator to 1MB boundary */
262 pciauto_region_align(pci_prefetch, 0x100000);
263
264 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
265 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
266 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
David Feng6eefd522015-02-02 16:53:13 +0800267 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
268#ifdef CONFIG_SYS_PCI_64BIT
269 pci_hose_write_config_dword(hose, dev,
270 PCI_PREF_BASE_UPPER32,
271 pci_prefetch->bus_lower >> 32);
272#else
273 pci_hose_write_config_dword(hose, dev,
274 PCI_PREF_BASE_UPPER32,
275 0x0);
276#endif
Kumar Galaa1790122006-01-11 13:24:15 -0600277
278 cmdstat |= PCI_COMMAND_MEMORY;
279 } else {
280 /* We don't support prefetchable memory for now, so disable */
281 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
Matthew McClintocka4e11552006-06-28 10:44:23 -0500282 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
David Feng6eefd522015-02-02 16:53:13 +0800283 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
284 pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
285 pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
286 }
Kumar Galaa1790122006-01-11 13:24:15 -0600287 }
288
wdenk3c74e322004-02-22 23:46:08 +0000289 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000290 /* Round I/O allocator to 4KB boundary */
291 pciauto_region_align(pci_io, 0x1000);
292
293 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
294 (pci_io->bus_lower & 0x0000f000) >> 8);
295 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
296 (pci_io->bus_lower & 0xffff0000) >> 16);
297
298 cmdstat |= PCI_COMMAND_IO;
299 }
300
wdenkc6097192002-11-03 00:24:07 +0000301 /* Enable memory and I/O accesses, enable bus master */
Andrew Sharpaf778c62012-08-01 12:27:16 +0000302 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
303 cmdstat | PCI_COMMAND_MASTER);
wdenkc6097192002-11-03 00:24:07 +0000304}
305
Ed Swarthoutba5feb12007-07-11 14:51:48 -0500306void pciauto_postscan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000307 pci_dev_t dev, int sub_bus)
308{
Bin Mengd11d9ef2015-07-19 00:20:06 +0800309 struct pci_region *pci_mem;
310 struct pci_region *pci_prefetch;
311 struct pci_region *pci_io;
312
313#ifdef CONFIG_DM_PCI
314 /* The root controller has the region information */
315 struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
316
317 pci_mem = ctlr_hose->pci_mem;
318 pci_prefetch = ctlr_hose->pci_prefetch;
319 pci_io = ctlr_hose->pci_io;
320#else
321 pci_mem = hose->pci_mem;
322 pci_prefetch = hose->pci_prefetch;
323 pci_io = hose->pci_io;
324#endif
wdenkc6097192002-11-03 00:24:07 +0000325
326 /* Configure bus number registers */
Bin Meng95f3aa22015-07-19 00:20:03 +0800327#ifdef CONFIG_DM_PCI
328 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, sub_bus);
329#else
Ed Swarthoute8b85f32007-07-11 14:52:08 -0500330 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
331 sub_bus - hose->first_busno);
Bin Meng95f3aa22015-07-19 00:20:03 +0800332#endif
wdenkc6097192002-11-03 00:24:07 +0000333
wdenk3c74e322004-02-22 23:46:08 +0000334 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000335 /* Round memory allocator to 1MB boundary */
336 pciauto_region_align(pci_mem, 0x100000);
337
338 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000339 (pci_mem->bus_lower - 1) >> 16);
wdenkc6097192002-11-03 00:24:07 +0000340 }
341
Kumar Galaa1790122006-01-11 13:24:15 -0600342 if (pci_prefetch) {
David Feng6eefd522015-02-02 16:53:13 +0800343 u16 prefechable_64;
344
345 pci_hose_read_config_word(hose, dev,
346 PCI_PREF_MEMORY_LIMIT,
347 &prefechable_64);
348 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
349
Kumar Galaa1790122006-01-11 13:24:15 -0600350 /* Round memory allocator to 1MB boundary */
351 pciauto_region_align(pci_prefetch, 0x100000);
352
353 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000354 (pci_prefetch->bus_lower - 1) >> 16);
David Feng6eefd522015-02-02 16:53:13 +0800355 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
356#ifdef CONFIG_SYS_PCI_64BIT
357 pci_hose_write_config_dword(hose, dev,
358 PCI_PREF_LIMIT_UPPER32,
359 (pci_prefetch->bus_lower - 1) >> 32);
360#else
361 pci_hose_write_config_dword(hose, dev,
362 PCI_PREF_LIMIT_UPPER32,
363 0x0);
364#endif
Kumar Galaa1790122006-01-11 13:24:15 -0600365 }
366
wdenk3c74e322004-02-22 23:46:08 +0000367 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000368 /* Round I/O allocator to 4KB boundary */
369 pciauto_region_align(pci_io, 0x1000);
370
371 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000372 ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
wdenkc6097192002-11-03 00:24:07 +0000373 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000374 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
wdenkc6097192002-11-03 00:24:07 +0000375 }
376}
377
378/*
379 *
380 */
381
382void pciauto_config_init(struct pci_controller *hose)
383{
384 int i;
385
Thierry Reding010c4802013-09-20 15:50:50 +0200386 hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
wdenkc6097192002-11-03 00:24:07 +0000387
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000388 for (i = 0; i < hose->region_count; i++) {
wdenk3c74e322004-02-22 23:46:08 +0000389 switch(hose->regions[i].flags) {
wdenkc6097192002-11-03 00:24:07 +0000390 case PCI_REGION_IO:
391 if (!hose->pci_io ||
392 hose->pci_io->size < hose->regions[i].size)
393 hose->pci_io = hose->regions + i;
394 break;
395 case PCI_REGION_MEM:
396 if (!hose->pci_mem ||
397 hose->pci_mem->size < hose->regions[i].size)
398 hose->pci_mem = hose->regions + i;
399 break;
Kumar Galaa1790122006-01-11 13:24:15 -0600400 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
401 if (!hose->pci_prefetch ||
402 hose->pci_prefetch->size < hose->regions[i].size)
403 hose->pci_prefetch = hose->regions + i;
404 break;
wdenkc6097192002-11-03 00:24:07 +0000405 }
406 }
407
408
wdenk3c74e322004-02-22 23:46:08 +0000409 if (hose->pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000410 pciauto_region_init(hose->pci_mem);
411
Simon Glassda4b1592015-07-31 09:31:33 -0600412 debug("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
Kumar Gala30e76d52008-10-21 08:36:08 -0500413 "\t\tPhysical Memory [%llx-%llxx]\n",
414 (u64)hose->pci_mem->bus_start,
415 (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
416 (u64)hose->pci_mem->phys_start,
417 (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
wdenkc6097192002-11-03 00:24:07 +0000418 }
419
Kumar Galaa1790122006-01-11 13:24:15 -0600420 if (hose->pci_prefetch) {
421 pciauto_region_init(hose->pci_prefetch);
422
Simon Glassda4b1592015-07-31 09:31:33 -0600423 debug("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
Kumar Gala30e76d52008-10-21 08:36:08 -0500424 "\t\tPhysical Memory [%llx-%llx]\n",
425 (u64)hose->pci_prefetch->bus_start,
426 (u64)(hose->pci_prefetch->bus_start +
427 hose->pci_prefetch->size - 1),
428 (u64)hose->pci_prefetch->phys_start,
429 (u64)(hose->pci_prefetch->phys_start +
430 hose->pci_prefetch->size - 1));
Kumar Galaa1790122006-01-11 13:24:15 -0600431 }
432
wdenk3c74e322004-02-22 23:46:08 +0000433 if (hose->pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000434 pciauto_region_init(hose->pci_io);
435
Simon Glassda4b1592015-07-31 09:31:33 -0600436 debug("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
Kumar Gala30e76d52008-10-21 08:36:08 -0500437 "\t\tPhysical Memory: [%llx-%llx]\n",
438 (u64)hose->pci_io->bus_start,
439 (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
440 (u64)hose->pci_io->phys_start,
441 (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
Ed Swarthoutba5feb12007-07-11 14:51:48 -0500442
wdenkc6097192002-11-03 00:24:07 +0000443 }
444}
445
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000446/*
447 * HJF: Changed this to return int. I think this is required
wdenkc7de8292002-11-19 11:04:11 +0000448 * to get the correct result when scanning bridges
449 */
450int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
wdenkc6097192002-11-03 00:24:07 +0000451{
Bin Mengd11d9ef2015-07-19 00:20:06 +0800452 struct pci_region *pci_mem;
453 struct pci_region *pci_prefetch;
454 struct pci_region *pci_io;
wdenkc7de8292002-11-19 11:04:11 +0000455 unsigned int sub_bus = PCI_BUS(dev);
wdenkc6097192002-11-03 00:24:07 +0000456 unsigned short class;
wdenk5653fc32004-02-08 22:55:38 +0000457 int n;
wdenkc6097192002-11-03 00:24:07 +0000458
Bin Mengd11d9ef2015-07-19 00:20:06 +0800459#ifdef CONFIG_DM_PCI
460 /* The root controller has the region information */
461 struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
462
463 pci_mem = ctlr_hose->pci_mem;
464 pci_prefetch = ctlr_hose->pci_prefetch;
465 pci_io = ctlr_hose->pci_io;
466#else
467 pci_mem = hose->pci_mem;
468 pci_prefetch = hose->pci_prefetch;
469 pci_io = hose->pci_io;
470#endif
471
wdenkc6097192002-11-03 00:24:07 +0000472 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
473
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000474 switch (class) {
wdenkc6097192002-11-03 00:24:07 +0000475 case PCI_CLASS_BRIDGE_PCI:
Simon Glassda4b1592015-07-31 09:31:33 -0600476 debug("PCI Autoconfig: Found P2P bridge, device %d\n",
477 PCI_DEV(dev));
Simon Glassff3e0772015-03-05 12:25:25 -0700478
Bin Mengd11d9ef2015-07-19 00:20:06 +0800479 pciauto_setup_device(hose, dev, 2, pci_mem,
480 pci_prefetch, pci_io);
wdenkc6097192002-11-03 00:24:07 +0000481
Simon Glassff3e0772015-03-05 12:25:25 -0700482#ifdef CONFIG_DM_PCI
483 n = dm_pci_hose_probe_bus(hose, dev);
484 if (n < 0)
485 return n;
486 sub_bus = (unsigned int)n;
487#else
wdenk3c74e322004-02-22 23:46:08 +0000488 /* Passing in current_busno allows for sibling P2P bridges */
Simon Glassff3e0772015-03-05 12:25:25 -0700489 hose->current_busno++;
wdenk5653fc32004-02-08 22:55:38 +0000490 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
wdenkcd37d9e2004-02-10 00:03:41 +0000491 /*
wdenk3c74e322004-02-22 23:46:08 +0000492 * need to figure out if this is a subordinate bridge on the bus
wdenk5653fc32004-02-08 22:55:38 +0000493 * to be able to properly set the pri/sec/sub bridge registers.
494 */
495 n = pci_hose_scan_bus(hose, hose->current_busno);
wdenk8bde7f72003-06-27 21:31:46 +0000496
wdenk3c74e322004-02-22 23:46:08 +0000497 /* figure out the deepest we've gone for this leg */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900498 sub_bus = max((unsigned int)n, sub_bus);
wdenkdb2f721f2003-03-06 00:58:30 +0000499 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
wdenk5653fc32004-02-08 22:55:38 +0000500
wdenkdb2f721f2003-03-06 00:58:30 +0000501 sub_bus = hose->current_busno;
Simon Glassff3e0772015-03-05 12:25:25 -0700502#endif
wdenkc6097192002-11-03 00:24:07 +0000503 break;
504
wdenk1cb8e982003-03-06 21:55:29 +0000505 case PCI_CLASS_BRIDGE_CARDBUS:
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000506 /*
507 * just do a minimal setup of the bridge,
508 * let the OS take care of the rest
509 */
Bin Mengd11d9ef2015-07-19 00:20:06 +0800510 pciauto_setup_device(hose, dev, 0, pci_mem,
511 pci_prefetch, pci_io);
wdenk1cb8e982003-03-06 21:55:29 +0000512
Simon Glassda4b1592015-07-31 09:31:33 -0600513 debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
514 PCI_DEV(dev));
wdenk1cb8e982003-03-06 21:55:29 +0000515
Simon Glassff3e0772015-03-05 12:25:25 -0700516#ifndef CONFIG_DM_PCI
wdenk1cb8e982003-03-06 21:55:29 +0000517 hose->current_busno++;
Simon Glassff3e0772015-03-05 12:25:25 -0700518#endif
wdenk1cb8e982003-03-06 21:55:29 +0000519 break;
520
TsiChung Liewf33fca22008-03-30 01:19:06 -0500521#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
wdenke0ac62d2003-08-17 18:55:18 +0000522 case PCI_CLASS_BRIDGE_OTHER:
Simon Glassda4b1592015-07-31 09:31:33 -0600523 debug("PCI Autoconfig: Skipping bridge device %d\n",
524 PCI_DEV(dev));
wdenke0ac62d2003-08-17 18:55:18 +0000525 break;
526#endif
Reinhard Arltc2e49f72009-07-25 06:19:12 +0200527#if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
Rafal Jaworowski6902df52005-10-17 02:39:53 +0200528 case PCI_CLASS_BRIDGE_OTHER:
529 /*
530 * The host/PCI bridge 1 seems broken in 8349 - it presents
531 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
532 * device claiming resources io/mem/irq.. we only allow for
533 * the PIMMR window to be allocated (BAR0 - 1MB size)
534 */
Simon Glassda4b1592015-07-31 09:31:33 -0600535 debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
Andrew Sharpcb2bf932012-08-29 14:16:29 +0000536 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
537 hose->pci_prefetch, hose->pci_io);
Rafal Jaworowski6902df52005-10-17 02:39:53 +0200538 break;
539#endif
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000540
541 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
Simon Glassda4b1592015-07-31 09:31:33 -0600542 debug("PCI AutoConfig: Found PowerPC device\n");
Andrew Sharp69fd2d32012-08-29 14:16:32 +0000543
wdenkc6097192002-11-03 00:24:07 +0000544 default:
Bin Mengd11d9ef2015-07-19 00:20:06 +0800545 pciauto_setup_device(hose, dev, 6, pci_mem,
546 pci_prefetch, pci_io);
wdenkc6097192002-11-03 00:24:07 +0000547 break;
548 }
wdenkc7de8292002-11-19 11:04:11 +0000549
550 return sub_bus;
wdenkc6097192002-11-03 00:24:07 +0000551}