blob: 79f27c744b295c4d25aeaddd99e9a87b23427c17 [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
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Jennejohn9a1263f2007-08-31 15:21:46 +020020#endif
21
wdenkc6097192002-11-03 00:24:07 +000022/*
23 *
24 */
25
Andrew Sharp68705132012-08-29 14:16:29 +000026void pciauto_region_init(struct pci_region *res)
wdenkc6097192002-11-03 00:24:07 +000027{
Sergei Shtylyov9679f4d2007-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 Galaad714f52008-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 Sharp68705132012-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 Galaad714f52008-10-21 08:36:08 -050044 pci_addr_t addr;
wdenkc6097192002-11-03 00:24:07 +000045
wdenk56ed43e2004-02-22 23:46:08 +000046 if (!res) {
Simon Glass927c1042015-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
wdenk56ed43e2004-02-22 23:46:08 +000053 if (addr - res->bus_start + size > res->size) {
Simon Glass927c1042015-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 Glass927c1042015-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 Galaad714f52008-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 Galae5ce4202006-01-11 13:24:15 -060078 struct pci_region *prefetch,
wdenkc6097192002-11-03 00:24:07 +000079 struct pci_region *io)
80{
Kumar Gala1873d5c2012-09-19 04:47:36 +000081 u32 bar_response;
Kumar Galaad714f52008-10-21 08:36:08 -050082 pci_size_t bar_size;
Andrew Sharpf4f24822012-08-01 12:27:16 +000083 u16 cmdstat = 0;
wdenkc6097192002-11-03 00:24:07 +000084 int bar, bar_nr = 0;
Simon Glass2b9acba2015-07-31 09:31:34 -060085#ifndef CONFIG_PCI_ENUM_ONLY
Bin Meng51e98ca2015-07-08 13:06:40 +080086 u8 header_type;
87 int rom_addr;
Andrew Sharp61d47ca2012-08-29 14:16:32 +000088 pci_addr_t bar_value;
89 struct pci_region *bar_res;
wdenkc6097192002-11-03 00:24:07 +000090 int found_mem64 = 0;
Andrew Sharp61d47ca2012-08-29 14:16:32 +000091#endif
wdenkc6097192002-11-03 00:24:07 +000092
Andrew Sharpf4f24822012-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 Sharp68705132012-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 Sharp61d47ca2012-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 Sharp61d47ca2012-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 Sharp61d47ca2012-08-29 14:16:32 +0000108#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000109 found_mem64 = 0;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000110#endif
wdenkc6097192002-11-03 00:24:07 +0000111
112 /* Check the BAR type and set our address mask */
wdenk56ed43e2004-02-22 23:46:08 +0000113 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Jin Zhengxiong-R64188f4ff3e82006-06-27 18:12:02 +0800114 bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
115 & 0xffff) + 1;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000116#ifndef CONFIG_PCI_ENUM_ONLY
wdenkc6097192002-11-03 00:24:07 +0000117 bar_res = io;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000118#endif
wdenkc6097192002-11-03 00:24:07 +0000119
Simon Glass927c1042015-07-31 09:31:33 -0600120 debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ",
121 bar_nr, (unsigned long long)bar_size);
wdenk56ed43e2004-02-22 23:46:08 +0000122 } else {
Andrew Sharp68705132012-08-29 14:16:29 +0000123 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
Kumar Galaad714f52008-10-21 08:36:08 -0500124 PCI_BASE_ADDRESS_MEM_TYPE_64) {
125 u32 bar_response_upper;
126 u64 bar64;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000127
128#ifndef CONFIG_PCI_ENUM_ONLY
Andrew Sharp68705132012-08-29 14:16:29 +0000129 pci_hose_write_config_dword(hose, dev, bar + 4,
130 0xffffffff);
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000131#endif
Andrew Sharp68705132012-08-29 14:16:29 +0000132 pci_hose_read_config_dword(hose, dev, bar + 4,
133 &bar_response_upper);
Kumar Galaad714f52008-10-21 08:36:08 -0500134
135 bar64 = ((u64)bar_response_upper << 32) | bar_response;
wdenkc6097192002-11-03 00:24:07 +0000136
Kumar Galaad714f52008-10-21 08:36:08 -0500137 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000138#ifndef CONFIG_PCI_ENUM_ONLY
Kumar Galaad714f52008-10-21 08:36:08 -0500139 found_mem64 = 1;
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000140#endif
Kumar Galaad714f52008-10-21 08:36:08 -0500141 } else {
142 bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
143 }
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000144#ifndef CONFIG_PCI_ENUM_ONLY
Kumar Galae5ce4202006-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 Sharp61d47ca2012-08-29 14:16:32 +0000149#endif
wdenkc6097192002-11-03 00:24:07 +0000150
Simon Glassa292d2a2015-07-27 15:47:18 -0600151 debug("PCI Autoconfig: BAR %d, %s, size=0x%llx, ",
152 bar_nr, bar_res == prefetch ? "Prf" : "Mem",
153 (unsigned long long)bar_size);
wdenkc6097192002-11-03 00:24:07 +0000154 }
155
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000156#ifndef CONFIG_PCI_ENUM_ONLY
wdenk56ed43e2004-02-22 23:46:08 +0000157 if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000158 /* Write it out and update our limit */
Kumar Galaad714f52008-10-21 08:36:08 -0500159 pci_hose_write_config_dword(hose, dev, bar, (u32)bar_value);
wdenkc6097192002-11-03 00:24:07 +0000160
wdenk56ed43e2004-02-22 23:46:08 +0000161 if (found_mem64) {
wdenkc6097192002-11-03 00:24:07 +0000162 bar += 4;
Kumar Galaad714f52008-10-21 08:36:08 -0500163#ifdef CONFIG_SYS_PCI_64BIT
164 pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
165#else
166 /*
167 * If we are a 64-bit decoder then increment to the
168 * upper 32 bits of the bar and force it to locate
169 * in the lower 4GB of memory.
170 */
wdenkc6097192002-11-03 00:24:07 +0000171 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
Kumar Galaad714f52008-10-21 08:36:08 -0500172#endif
wdenkc6097192002-11-03 00:24:07 +0000173 }
174
wdenkc6097192002-11-03 00:24:07 +0000175 }
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000176#endif
177 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
178 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
wdenkc6097192002-11-03 00:24:07 +0000179
Simon Glass927c1042015-07-31 09:31:33 -0600180 debug("\n");
wdenkc6097192002-11-03 00:24:07 +0000181
182 bar_nr++;
183 }
184
Simon Glass2b9acba2015-07-31 09:31:34 -0600185#ifndef CONFIG_PCI_ENUM_ONLY
Bin Meng51e98ca2015-07-08 13:06:40 +0800186 /* Configure the expansion ROM address */
187 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
Bin Menge8bd7462015-10-07 02:13:18 -0700188 header_type &= 0x7f;
Bin Meng51e98ca2015-07-08 13:06:40 +0800189 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);
Simon Glass927c1042015-07-31 09:31:33 -0600196 debug("PCI Autoconfig: ROM, size=%#x, ",
197 (unsigned int)bar_size);
Bin Meng51e98ca2015-07-08 13:06:40 +0800198 if (pciauto_region_allocate(mem, bar_size,
199 &bar_value) == 0) {
200 pci_hose_write_config_dword(hose, dev, rom_addr,
201 bar_value);
202 }
203 cmdstat |= PCI_COMMAND_MEMORY;
Simon Glass927c1042015-07-31 09:31:33 -0600204 debug("\n");
Bin Meng51e98ca2015-07-08 13:06:40 +0800205 }
206 }
Simon Glass2b9acba2015-07-31 09:31:34 -0600207#endif
Bin Meng51e98ca2015-07-08 13:06:40 +0800208
Andrew Sharpf4f24822012-08-01 12:27:16 +0000209 pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
Gary Jennejohn9a1263f2007-08-31 15:21:46 +0200210 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200211 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000212 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
213}
214
Ed Swarthouta5232962007-07-11 14:51:48 -0500215void pciauto_prescan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000216 pci_dev_t dev, int sub_bus)
217{
Bin Meng39164092015-07-19 00:20:06 +0800218 struct pci_region *pci_mem;
219 struct pci_region *pci_prefetch;
220 struct pci_region *pci_io;
David Feng3be54fd2015-02-02 16:53:13 +0800221 u16 cmdstat, prefechable_64;
wdenkc6097192002-11-03 00:24:07 +0000222
Bin Meng39164092015-07-19 00:20:06 +0800223#ifdef CONFIG_DM_PCI
224 /* The root controller has the region information */
225 struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
226
227 pci_mem = ctlr_hose->pci_mem;
228 pci_prefetch = ctlr_hose->pci_prefetch;
229 pci_io = ctlr_hose->pci_io;
230#else
231 pci_mem = hose->pci_mem;
232 pci_prefetch = hose->pci_prefetch;
233 pci_io = hose->pci_io;
234#endif
235
Andrew Sharpf4f24822012-08-01 12:27:16 +0000236 pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
David Feng3be54fd2015-02-02 16:53:13 +0800237 pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
238 &prefechable_64);
239 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
wdenkc6097192002-11-03 00:24:07 +0000240
241 /* Configure bus number registers */
Bin Meng07bd3232015-07-19 00:20:03 +0800242#ifdef CONFIG_DM_PCI
243 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, PCI_BUS(dev));
244 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, sub_bus);
245#else
Ed Swarthout4aeb55a2007-07-11 14:52:08 -0500246 pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
247 PCI_BUS(dev) - hose->first_busno);
248 pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
249 sub_bus - hose->first_busno);
Bin Meng07bd3232015-07-19 00:20:03 +0800250#endif
wdenkc6097192002-11-03 00:24:07 +0000251 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
252
wdenk56ed43e2004-02-22 23:46:08 +0000253 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000254 /* Round memory allocator to 1MB boundary */
255 pciauto_region_align(pci_mem, 0x100000);
256
257 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
258 pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
259 (pci_mem->bus_lower & 0xfff00000) >> 16);
260
261 cmdstat |= PCI_COMMAND_MEMORY;
262 }
263
Kumar Galae5ce4202006-01-11 13:24:15 -0600264 if (pci_prefetch) {
265 /* Round memory allocator to 1MB boundary */
266 pciauto_region_align(pci_prefetch, 0x100000);
267
268 /* Set up memory and I/O filter limits, assume 32-bit I/O space */
269 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
270 (pci_prefetch->bus_lower & 0xfff00000) >> 16);
David Feng3be54fd2015-02-02 16:53:13 +0800271 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
272#ifdef CONFIG_SYS_PCI_64BIT
273 pci_hose_write_config_dword(hose, dev,
274 PCI_PREF_BASE_UPPER32,
275 pci_prefetch->bus_lower >> 32);
276#else
277 pci_hose_write_config_dword(hose, dev,
278 PCI_PREF_BASE_UPPER32,
279 0x0);
280#endif
Kumar Galae5ce4202006-01-11 13:24:15 -0600281
282 cmdstat |= PCI_COMMAND_MEMORY;
283 } else {
284 /* We don't support prefetchable memory for now, so disable */
285 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
Matthew McClintock2f43f332006-06-28 10:44:23 -0500286 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
David Feng3be54fd2015-02-02 16:53:13 +0800287 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
288 pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
289 pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
290 }
Kumar Galae5ce4202006-01-11 13:24:15 -0600291 }
292
wdenk56ed43e2004-02-22 23:46:08 +0000293 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000294 /* Round I/O allocator to 4KB boundary */
295 pciauto_region_align(pci_io, 0x1000);
296
297 pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
298 (pci_io->bus_lower & 0x0000f000) >> 8);
299 pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
300 (pci_io->bus_lower & 0xffff0000) >> 16);
301
302 cmdstat |= PCI_COMMAND_IO;
303 }
304
wdenkc6097192002-11-03 00:24:07 +0000305 /* Enable memory and I/O accesses, enable bus master */
Andrew Sharpf4f24822012-08-01 12:27:16 +0000306 pci_hose_write_config_word(hose, dev, PCI_COMMAND,
307 cmdstat | PCI_COMMAND_MASTER);
wdenkc6097192002-11-03 00:24:07 +0000308}
309
Ed Swarthouta5232962007-07-11 14:51:48 -0500310void pciauto_postscan_setup_bridge(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000311 pci_dev_t dev, int sub_bus)
312{
Bin Meng39164092015-07-19 00:20:06 +0800313 struct pci_region *pci_mem;
314 struct pci_region *pci_prefetch;
315 struct pci_region *pci_io;
316
317#ifdef CONFIG_DM_PCI
318 /* The root controller has the region information */
319 struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
320
321 pci_mem = ctlr_hose->pci_mem;
322 pci_prefetch = ctlr_hose->pci_prefetch;
323 pci_io = ctlr_hose->pci_io;
324#else
325 pci_mem = hose->pci_mem;
326 pci_prefetch = hose->pci_prefetch;
327 pci_io = hose->pci_io;
328#endif
wdenkc6097192002-11-03 00:24:07 +0000329
330 /* Configure bus number registers */
Bin Meng07bd3232015-07-19 00:20:03 +0800331#ifdef CONFIG_DM_PCI
332 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, sub_bus);
333#else
Ed Swarthout4aeb55a2007-07-11 14:52:08 -0500334 pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
335 sub_bus - hose->first_busno);
Bin Meng07bd3232015-07-19 00:20:03 +0800336#endif
wdenkc6097192002-11-03 00:24:07 +0000337
wdenk56ed43e2004-02-22 23:46:08 +0000338 if (pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000339 /* Round memory allocator to 1MB boundary */
340 pciauto_region_align(pci_mem, 0x100000);
341
342 pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
Andrew Sharp68705132012-08-29 14:16:29 +0000343 (pci_mem->bus_lower - 1) >> 16);
wdenkc6097192002-11-03 00:24:07 +0000344 }
345
Kumar Galae5ce4202006-01-11 13:24:15 -0600346 if (pci_prefetch) {
David Feng3be54fd2015-02-02 16:53:13 +0800347 u16 prefechable_64;
348
349 pci_hose_read_config_word(hose, dev,
350 PCI_PREF_MEMORY_LIMIT,
351 &prefechable_64);
352 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
353
Kumar Galae5ce4202006-01-11 13:24:15 -0600354 /* Round memory allocator to 1MB boundary */
355 pciauto_region_align(pci_prefetch, 0x100000);
356
357 pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
Andrew Sharp68705132012-08-29 14:16:29 +0000358 (pci_prefetch->bus_lower - 1) >> 16);
David Feng3be54fd2015-02-02 16:53:13 +0800359 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
360#ifdef CONFIG_SYS_PCI_64BIT
361 pci_hose_write_config_dword(hose, dev,
362 PCI_PREF_LIMIT_UPPER32,
363 (pci_prefetch->bus_lower - 1) >> 32);
364#else
365 pci_hose_write_config_dword(hose, dev,
366 PCI_PREF_LIMIT_UPPER32,
367 0x0);
368#endif
Kumar Galae5ce4202006-01-11 13:24:15 -0600369 }
370
wdenk56ed43e2004-02-22 23:46:08 +0000371 if (pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000372 /* Round I/O allocator to 4KB boundary */
373 pciauto_region_align(pci_io, 0x1000);
374
375 pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
Andrew Sharp68705132012-08-29 14:16:29 +0000376 ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
wdenkc6097192002-11-03 00:24:07 +0000377 pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
Andrew Sharp68705132012-08-29 14:16:29 +0000378 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
wdenkc6097192002-11-03 00:24:07 +0000379 }
380}
381
382/*
383 *
384 */
385
386void pciauto_config_init(struct pci_controller *hose)
387{
388 int i;
389
Thierry Redinga3d5df32013-09-20 15:50:50 +0200390 hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
wdenkc6097192002-11-03 00:24:07 +0000391
Andrew Sharp68705132012-08-29 14:16:29 +0000392 for (i = 0; i < hose->region_count; i++) {
wdenk56ed43e2004-02-22 23:46:08 +0000393 switch(hose->regions[i].flags) {
wdenkc6097192002-11-03 00:24:07 +0000394 case PCI_REGION_IO:
395 if (!hose->pci_io ||
396 hose->pci_io->size < hose->regions[i].size)
397 hose->pci_io = hose->regions + i;
398 break;
399 case PCI_REGION_MEM:
400 if (!hose->pci_mem ||
401 hose->pci_mem->size < hose->regions[i].size)
402 hose->pci_mem = hose->regions + i;
403 break;
Kumar Galae5ce4202006-01-11 13:24:15 -0600404 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
405 if (!hose->pci_prefetch ||
406 hose->pci_prefetch->size < hose->regions[i].size)
407 hose->pci_prefetch = hose->regions + i;
408 break;
wdenkc6097192002-11-03 00:24:07 +0000409 }
410 }
411
412
wdenk56ed43e2004-02-22 23:46:08 +0000413 if (hose->pci_mem) {
wdenkc6097192002-11-03 00:24:07 +0000414 pciauto_region_init(hose->pci_mem);
415
Simon Glass927c1042015-07-31 09:31:33 -0600416 debug("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
Kumar Galaad714f52008-10-21 08:36:08 -0500417 "\t\tPhysical Memory [%llx-%llxx]\n",
418 (u64)hose->pci_mem->bus_start,
419 (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
420 (u64)hose->pci_mem->phys_start,
421 (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
wdenkc6097192002-11-03 00:24:07 +0000422 }
423
Kumar Galae5ce4202006-01-11 13:24:15 -0600424 if (hose->pci_prefetch) {
425 pciauto_region_init(hose->pci_prefetch);
426
Simon Glass927c1042015-07-31 09:31:33 -0600427 debug("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
Kumar Galaad714f52008-10-21 08:36:08 -0500428 "\t\tPhysical Memory [%llx-%llx]\n",
429 (u64)hose->pci_prefetch->bus_start,
430 (u64)(hose->pci_prefetch->bus_start +
431 hose->pci_prefetch->size - 1),
432 (u64)hose->pci_prefetch->phys_start,
433 (u64)(hose->pci_prefetch->phys_start +
434 hose->pci_prefetch->size - 1));
Kumar Galae5ce4202006-01-11 13:24:15 -0600435 }
436
wdenk56ed43e2004-02-22 23:46:08 +0000437 if (hose->pci_io) {
wdenkc6097192002-11-03 00:24:07 +0000438 pciauto_region_init(hose->pci_io);
439
Simon Glass927c1042015-07-31 09:31:33 -0600440 debug("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
Kumar Galaad714f52008-10-21 08:36:08 -0500441 "\t\tPhysical Memory: [%llx-%llx]\n",
442 (u64)hose->pci_io->bus_start,
443 (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
444 (u64)hose->pci_io->phys_start,
445 (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
Ed Swarthouta5232962007-07-11 14:51:48 -0500446
wdenkc6097192002-11-03 00:24:07 +0000447 }
448}
449
Andrew Sharp68705132012-08-29 14:16:29 +0000450/*
451 * HJF: Changed this to return int. I think this is required
wdenk452cfd62002-11-19 11:04:11 +0000452 * to get the correct result when scanning bridges
453 */
454int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
wdenkc6097192002-11-03 00:24:07 +0000455{
Bin Meng39164092015-07-19 00:20:06 +0800456 struct pci_region *pci_mem;
457 struct pci_region *pci_prefetch;
458 struct pci_region *pci_io;
wdenk452cfd62002-11-19 11:04:11 +0000459 unsigned int sub_bus = PCI_BUS(dev);
wdenkc6097192002-11-03 00:24:07 +0000460 unsigned short class;
wdenk2cefd152004-02-08 22:55:38 +0000461 int n;
wdenkc6097192002-11-03 00:24:07 +0000462
Bin Meng39164092015-07-19 00:20:06 +0800463#ifdef CONFIG_DM_PCI
464 /* The root controller has the region information */
465 struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
466
467 pci_mem = ctlr_hose->pci_mem;
468 pci_prefetch = ctlr_hose->pci_prefetch;
469 pci_io = ctlr_hose->pci_io;
470#else
471 pci_mem = hose->pci_mem;
472 pci_prefetch = hose->pci_prefetch;
473 pci_io = hose->pci_io;
474#endif
475
wdenkc6097192002-11-03 00:24:07 +0000476 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
477
Andrew Sharp68705132012-08-29 14:16:29 +0000478 switch (class) {
wdenkc6097192002-11-03 00:24:07 +0000479 case PCI_CLASS_BRIDGE_PCI:
Simon Glass927c1042015-07-31 09:31:33 -0600480 debug("PCI Autoconfig: Found P2P bridge, device %d\n",
481 PCI_DEV(dev));
Simon Glassb94dc892015-03-05 12:25:25 -0700482
Bin Meng39164092015-07-19 00:20:06 +0800483 pciauto_setup_device(hose, dev, 2, pci_mem,
484 pci_prefetch, pci_io);
wdenkc6097192002-11-03 00:24:07 +0000485
Simon Glassb94dc892015-03-05 12:25:25 -0700486#ifdef CONFIG_DM_PCI
487 n = dm_pci_hose_probe_bus(hose, dev);
488 if (n < 0)
489 return n;
490 sub_bus = (unsigned int)n;
491#else
wdenk56ed43e2004-02-22 23:46:08 +0000492 /* Passing in current_busno allows for sibling P2P bridges */
Simon Glassb94dc892015-03-05 12:25:25 -0700493 hose->current_busno++;
wdenk2cefd152004-02-08 22:55:38 +0000494 pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
wdenk6cfa84e2004-02-10 00:03:41 +0000495 /*
wdenk56ed43e2004-02-22 23:46:08 +0000496 * need to figure out if this is a subordinate bridge on the bus
wdenk2cefd152004-02-08 22:55:38 +0000497 * to be able to properly set the pri/sec/sub bridge registers.
498 */
499 n = pci_hose_scan_bus(hose, hose->current_busno);
wdenk57b2d802003-06-27 21:31:46 +0000500
wdenk56ed43e2004-02-22 23:46:08 +0000501 /* figure out the deepest we've gone for this leg */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900502 sub_bus = max((unsigned int)n, sub_bus);
wdenkb666c8f2003-03-06 00:58:30 +0000503 pciauto_postscan_setup_bridge(hose, dev, sub_bus);
wdenk2cefd152004-02-08 22:55:38 +0000504
wdenkb666c8f2003-03-06 00:58:30 +0000505 sub_bus = hose->current_busno;
Simon Glassb94dc892015-03-05 12:25:25 -0700506#endif
wdenkc6097192002-11-03 00:24:07 +0000507 break;
508
wdenk1fe2c702003-03-06 21:55:29 +0000509 case PCI_CLASS_BRIDGE_CARDBUS:
Andrew Sharp68705132012-08-29 14:16:29 +0000510 /*
511 * just do a minimal setup of the bridge,
512 * let the OS take care of the rest
513 */
Bin Meng39164092015-07-19 00:20:06 +0800514 pciauto_setup_device(hose, dev, 0, pci_mem,
515 pci_prefetch, pci_io);
wdenk1fe2c702003-03-06 21:55:29 +0000516
Simon Glass927c1042015-07-31 09:31:33 -0600517 debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
518 PCI_DEV(dev));
wdenk1fe2c702003-03-06 21:55:29 +0000519
Simon Glassb94dc892015-03-05 12:25:25 -0700520#ifndef CONFIG_DM_PCI
wdenk1fe2c702003-03-06 21:55:29 +0000521 hose->current_busno++;
Simon Glassb94dc892015-03-05 12:25:25 -0700522#endif
wdenk1fe2c702003-03-06 21:55:29 +0000523 break;
524
TsiChung Liew521f97b2008-03-30 01:19:06 -0500525#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
wdenk5d841732003-08-17 18:55:18 +0000526 case PCI_CLASS_BRIDGE_OTHER:
Simon Glass927c1042015-07-31 09:31:33 -0600527 debug("PCI Autoconfig: Skipping bridge device %d\n",
528 PCI_DEV(dev));
wdenk5d841732003-08-17 18:55:18 +0000529 break;
530#endif
Reinhard Arlt46911792009-07-25 06:19:12 +0200531#if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200532 case PCI_CLASS_BRIDGE_OTHER:
533 /*
534 * The host/PCI bridge 1 seems broken in 8349 - it presents
535 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
536 * device claiming resources io/mem/irq.. we only allow for
537 * the PIMMR window to be allocated (BAR0 - 1MB size)
538 */
Simon Glass927c1042015-07-31 09:31:33 -0600539 debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
Andrew Sharp68705132012-08-29 14:16:29 +0000540 pciauto_setup_device(hose, dev, 0, hose->pci_mem,
541 hose->pci_prefetch, hose->pci_io);
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200542 break;
543#endif
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000544
545 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
Simon Glass927c1042015-07-31 09:31:33 -0600546 debug("PCI AutoConfig: Found PowerPC device\n");
Andrew Sharp61d47ca2012-08-29 14:16:32 +0000547
wdenkc6097192002-11-03 00:24:07 +0000548 default:
Bin Meng39164092015-07-19 00:20:06 +0800549 pciauto_setup_device(hose, dev, 6, pci_mem,
550 pci_prefetch, pci_io);
wdenkc6097192002-11-03 00:24:07 +0000551 break;
552 }
wdenk452cfd62002-11-19 11:04:11 +0000553
554 return sub_bus;
wdenkc6097192002-11-03 00:24:07 +0000555}