blob: 7b6e629cae702e85b6ff55623db0fc5519160c3f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5e23b8b2015-11-29 13:17:49 -07002/*
3 * PCI autoconfiguration library
4 *
5 * Author: Matt Porter <mporter@mvista.com>
6 *
7 * Copyright 2000 MontaVista Software Inc.
Simon Glass5e23b8b2015-11-29 13:17:49 -07008 */
9
10#include <common.h>
Simon Glass4439bc32016-01-18 20:19:16 -070011#include <dm.h>
Simon Glass5e23b8b2015-11-29 13:17:49 -070012#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass5e23b8b2015-11-29 13:17:49 -070014#include <pci.h>
15
16/* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
17#ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
18#define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
19#endif
20
Stefan Roesea7a029d2021-01-12 12:03:43 +010021static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
22 struct pci_region *mem,
23 struct pci_region *prefetch,
24 struct pci_region *io)
Simon Glass5e23b8b2015-11-29 13:17:49 -070025{
26 u32 bar_response;
27 pci_size_t bar_size;
28 u16 cmdstat = 0;
29 int bar, bar_nr = 0;
30 u8 header_type;
31 int rom_addr;
32 pci_addr_t bar_value;
Bin Meng67967042016-02-17 23:14:47 -080033 struct pci_region *bar_res = NULL;
Simon Glass5e23b8b2015-11-29 13:17:49 -070034 int found_mem64 = 0;
35 u16 class;
36
37 dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
38 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
39 PCI_COMMAND_MASTER;
40
41 for (bar = PCI_BASE_ADDRESS_0;
42 bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
Simon Glass6a73cf32019-09-25 08:56:16 -060043 int ret = 0;
44
Simon Glass5e23b8b2015-11-29 13:17:49 -070045 /* Tickle the BAR and get the response */
Stefan Roesea7a029d2021-01-12 12:03:43 +010046 dm_pci_write_config32(dev, bar, 0xffffffff);
Simon Glass5e23b8b2015-11-29 13:17:49 -070047 dm_pci_read_config32(dev, bar, &bar_response);
48
Phil Sutterc1b12632021-01-03 23:06:45 +010049 /* If BAR is not implemented (or invalid) go to the next BAR */
50 if (!bar_response || bar_response == 0xffffffff)
Simon Glass5e23b8b2015-11-29 13:17:49 -070051 continue;
52
53 found_mem64 = 0;
54
55 /* Check the BAR type and set our address mask */
56 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Phil Sutterc1b12632021-01-03 23:06:45 +010057 bar_size = bar_response & PCI_BASE_ADDRESS_IO_MASK;
58 bar_size &= ~(bar_size - 1);
59
Stefan Roesea7a029d2021-01-12 12:03:43 +010060 bar_res = io;
Simon Glass5e23b8b2015-11-29 13:17:49 -070061
62 debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ",
63 bar_nr, (unsigned long long)bar_size);
64 } else {
65 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
66 PCI_BASE_ADDRESS_MEM_TYPE_64) {
67 u32 bar_response_upper;
68 u64 bar64;
69
Stefan Roesea7a029d2021-01-12 12:03:43 +010070 dm_pci_write_config32(dev, bar + 4, 0xffffffff);
Simon Glass5e23b8b2015-11-29 13:17:49 -070071 dm_pci_read_config32(dev, bar + 4,
72 &bar_response_upper);
73
74 bar64 = ((u64)bar_response_upper << 32) |
75 bar_response;
76
77 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK)
78 + 1;
Stefan Roesea7a029d2021-01-12 12:03:43 +010079 found_mem64 = 1;
Simon Glass5e23b8b2015-11-29 13:17:49 -070080 } else {
81 bar_size = (u32)(~(bar_response &
82 PCI_BASE_ADDRESS_MEM_MASK) + 1);
83 }
Stefan Roesea7a029d2021-01-12 12:03:43 +010084
85 if (prefetch &&
86 (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
87 bar_res = prefetch;
88 else
89 bar_res = mem;
Simon Glass5e23b8b2015-11-29 13:17:49 -070090
Phil Suttera62de442021-03-03 01:57:35 +010091 debug("PCI Autoconfig: BAR %d, %s%s, size=0x%llx, ",
Simon Glass5e23b8b2015-11-29 13:17:49 -070092 bar_nr, bar_res == prefetch ? "Prf" : "Mem",
Phil Suttera62de442021-03-03 01:57:35 +010093 found_mem64 ? "64" : "",
Simon Glass5e23b8b2015-11-29 13:17:49 -070094 (unsigned long long)bar_size);
95 }
96
Stefan Roesea7a029d2021-01-12 12:03:43 +010097 ret = pciauto_region_allocate(bar_res, bar_size,
98 &bar_value, found_mem64);
99 if (ret)
100 printf("PCI: Failed autoconfig bar %x\n", bar);
101
102 if (!ret) {
Simon Glass5e23b8b2015-11-29 13:17:49 -0700103 /* Write it out and update our limit */
104 dm_pci_write_config32(dev, bar, (u32)bar_value);
105
106 if (found_mem64) {
107 bar += 4;
108#ifdef CONFIG_SYS_PCI_64BIT
109 dm_pci_write_config32(dev, bar,
110 (u32)(bar_value >> 32));
111#else
112 /*
113 * If we are a 64-bit decoder then increment to
114 * the upper 32 bits of the bar and force it to
115 * locate in the lower 4GB of memory.
116 */
117 dm_pci_write_config32(dev, bar, 0x00000000);
118#endif
119 }
120 }
121
122 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
123 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
124
125 debug("\n");
126
127 bar_nr++;
128 }
129
Stefan Roesea7a029d2021-01-12 12:03:43 +0100130 /* Configure the expansion ROM address */
131 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
132 header_type &= 0x7f;
133 if (header_type != PCI_HEADER_TYPE_CARDBUS) {
134 rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
135 PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
136 dm_pci_write_config32(dev, rom_addr, 0xfffffffe);
137 dm_pci_read_config32(dev, rom_addr, &bar_response);
138 if (bar_response) {
139 bar_size = -(bar_response & ~1);
140 debug("PCI Autoconfig: ROM, size=%#x, ",
141 (unsigned int)bar_size);
142 if (pciauto_region_allocate(mem, bar_size, &bar_value,
143 false) == 0) {
144 dm_pci_write_config32(dev, rom_addr, bar_value);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700145 }
Stefan Roesea7a029d2021-01-12 12:03:43 +0100146 cmdstat |= PCI_COMMAND_MEMORY;
147 debug("\n");
Simon Glass5e23b8b2015-11-29 13:17:49 -0700148 }
149 }
150
151 /* PCI_COMMAND_IO must be set for VGA device */
152 dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
153 if (class == PCI_CLASS_DISPLAY_VGA)
154 cmdstat |= PCI_COMMAND_IO;
155
156 dm_pci_write_config16(dev, PCI_COMMAND, cmdstat);
157 dm_pci_write_config8(dev, PCI_CACHE_LINE_SIZE,
158 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
159 dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x80);
160}
161
162void dm_pciauto_prescan_setup_bridge(struct udevice *dev, int sub_bus)
163{
164 struct pci_region *pci_mem;
165 struct pci_region *pci_prefetch;
166 struct pci_region *pci_io;
167 u16 cmdstat, prefechable_64;
Pali Rohár8e85f362021-09-10 13:33:35 +0200168 u8 io_32;
Simon Glass4439bc32016-01-18 20:19:16 -0700169 struct udevice *ctlr = pci_get_controller(dev);
170 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700171
172 pci_mem = ctlr_hose->pci_mem;
173 pci_prefetch = ctlr_hose->pci_prefetch;
174 pci_io = ctlr_hose->pci_io;
175
176 dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
177 dm_pci_read_config16(dev, PCI_PREF_MEMORY_BASE, &prefechable_64);
178 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
Pali Rohár8e85f362021-09-10 13:33:35 +0200179 dm_pci_read_config8(dev, PCI_IO_LIMIT, &io_32);
180 io_32 &= PCI_IO_RANGE_TYPE_MASK;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700181
182 /* Configure bus number registers */
183 dm_pci_write_config8(dev, PCI_PRIMARY_BUS,
Simon Glass8b85dfc2020-12-16 21:20:07 -0700184 PCI_BUS(dm_pci_get_bdf(dev)) - dev_seq(ctlr));
185 dm_pci_write_config8(dev, PCI_SECONDARY_BUS, sub_bus - dev_seq(ctlr));
Simon Glass5e23b8b2015-11-29 13:17:49 -0700186 dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, 0xff);
187
188 if (pci_mem) {
189 /* Round memory allocator to 1MB boundary */
190 pciauto_region_align(pci_mem, 0x100000);
191
192 /*
193 * Set up memory and I/O filter limits, assume 32-bit
194 * I/O space
195 */
196 dm_pci_write_config16(dev, PCI_MEMORY_BASE,
Pali Rohár8e85f362021-09-10 13:33:35 +0200197 ((pci_mem->bus_lower & 0xfff00000) >> 16) &
198 PCI_MEMORY_RANGE_MASK);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700199
200 cmdstat |= PCI_COMMAND_MEMORY;
201 }
202
203 if (pci_prefetch) {
204 /* Round memory allocator to 1MB boundary */
205 pciauto_region_align(pci_prefetch, 0x100000);
206
207 /*
208 * Set up memory and I/O filter limits, assume 32-bit
209 * I/O space
210 */
211 dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE,
Pali Rohár8e85f362021-09-10 13:33:35 +0200212 (((pci_prefetch->bus_lower & 0xfff00000) >> 16) &
213 PCI_PREF_RANGE_MASK) | prefechable_64);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700214 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
215#ifdef CONFIG_SYS_PCI_64BIT
216 dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32,
217 pci_prefetch->bus_lower >> 32);
218#else
219 dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0x0);
220#endif
221
222 cmdstat |= PCI_COMMAND_MEMORY;
223 } else {
224 /* We don't support prefetchable memory for now, so disable */
Pali Rohár8e85f362021-09-10 13:33:35 +0200225 dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE, 0x1000 |
226 prefechable_64);
227 dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, 0x0 |
228 prefechable_64);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700229 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
230 dm_pci_write_config16(dev, PCI_PREF_BASE_UPPER32, 0x0);
231 dm_pci_write_config16(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
232 }
233 }
234
235 if (pci_io) {
236 /* Round I/O allocator to 4KB boundary */
237 pciauto_region_align(pci_io, 0x1000);
238
239 dm_pci_write_config8(dev, PCI_IO_BASE,
Pali Rohár8e85f362021-09-10 13:33:35 +0200240 (((pci_io->bus_lower & 0x0000f000) >> 8) &
241 PCI_IO_RANGE_MASK) | io_32);
242 if (io_32 == PCI_IO_RANGE_TYPE_32)
243 dm_pci_write_config16(dev, PCI_IO_BASE_UPPER16,
Simon Glass5e23b8b2015-11-29 13:17:49 -0700244 (pci_io->bus_lower & 0xffff0000) >> 16);
245
246 cmdstat |= PCI_COMMAND_IO;
247 }
248
249 /* Enable memory and I/O accesses, enable bus master */
250 dm_pci_write_config16(dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
251}
252
253void dm_pciauto_postscan_setup_bridge(struct udevice *dev, int sub_bus)
254{
255 struct pci_region *pci_mem;
256 struct pci_region *pci_prefetch;
257 struct pci_region *pci_io;
Simon Glass4439bc32016-01-18 20:19:16 -0700258 struct udevice *ctlr = pci_get_controller(dev);
259 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700260
261 pci_mem = ctlr_hose->pci_mem;
262 pci_prefetch = ctlr_hose->pci_prefetch;
263 pci_io = ctlr_hose->pci_io;
264
265 /* Configure bus number registers */
Simon Glass8b85dfc2020-12-16 21:20:07 -0700266 dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, sub_bus - dev_seq(ctlr));
Simon Glass5e23b8b2015-11-29 13:17:49 -0700267
268 if (pci_mem) {
269 /* Round memory allocator to 1MB boundary */
270 pciauto_region_align(pci_mem, 0x100000);
271
272 dm_pci_write_config16(dev, PCI_MEMORY_LIMIT,
Pali Rohár8e85f362021-09-10 13:33:35 +0200273 ((pci_mem->bus_lower - 1) >> 16) &
274 PCI_MEMORY_RANGE_MASK);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700275 }
276
277 if (pci_prefetch) {
278 u16 prefechable_64;
279
280 dm_pci_read_config16(dev, PCI_PREF_MEMORY_LIMIT,
281 &prefechable_64);
282 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
283
284 /* Round memory allocator to 1MB boundary */
285 pciauto_region_align(pci_prefetch, 0x100000);
286
287 dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT,
Pali Rohár8e85f362021-09-10 13:33:35 +0200288 (((pci_prefetch->bus_lower - 1) >> 16) &
289 PCI_PREF_RANGE_MASK) | prefechable_64);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700290 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
291#ifdef CONFIG_SYS_PCI_64BIT
292 dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32,
293 (pci_prefetch->bus_lower - 1) >> 32);
294#else
295 dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
296#endif
297 }
298
299 if (pci_io) {
Pali Rohár8e85f362021-09-10 13:33:35 +0200300 u8 io_32;
301
302 dm_pci_read_config8(dev, PCI_IO_LIMIT,
303 &io_32);
304 io_32 &= PCI_IO_RANGE_TYPE_MASK;
305
Simon Glass5e23b8b2015-11-29 13:17:49 -0700306 /* Round I/O allocator to 4KB boundary */
307 pciauto_region_align(pci_io, 0x1000);
308
309 dm_pci_write_config8(dev, PCI_IO_LIMIT,
Pali Rohár8e85f362021-09-10 13:33:35 +0200310 ((((pci_io->bus_lower - 1) & 0x0000f000) >> 8) &
311 PCI_IO_RANGE_MASK) | io_32);
312 if (io_32 == PCI_IO_RANGE_TYPE_32)
313 dm_pci_write_config16(dev, PCI_IO_LIMIT_UPPER16,
Simon Glass5e23b8b2015-11-29 13:17:49 -0700314 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
315 }
316}
317
318/*
319 * HJF: Changed this to return int. I think this is required
320 * to get the correct result when scanning bridges
321 */
322int dm_pciauto_config_device(struct udevice *dev)
323{
324 struct pci_region *pci_mem;
325 struct pci_region *pci_prefetch;
326 struct pci_region *pci_io;
327 unsigned int sub_bus = PCI_BUS(dm_pci_get_bdf(dev));
328 unsigned short class;
Simon Glass4439bc32016-01-18 20:19:16 -0700329 struct udevice *ctlr = pci_get_controller(dev);
330 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass42f36632020-12-16 21:20:18 -0700331 int ret;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700332
Simon Glass5e23b8b2015-11-29 13:17:49 -0700333 pci_mem = ctlr_hose->pci_mem;
334 pci_prefetch = ctlr_hose->pci_prefetch;
335 pci_io = ctlr_hose->pci_io;
336
337 dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
338
339 switch (class) {
340 case PCI_CLASS_BRIDGE_PCI:
341 debug("PCI Autoconfig: Found P2P bridge, device %d\n",
342 PCI_DEV(dm_pci_get_bdf(dev)));
343
Stefan Roesea7a029d2021-01-12 12:03:43 +0100344 dm_pciauto_setup_device(dev, 2, pci_mem, pci_prefetch, pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700345
Simon Glass42f36632020-12-16 21:20:18 -0700346 ret = dm_pci_hose_probe_bus(dev);
347 if (ret < 0)
348 return log_msg_ret("probe", ret);
349 sub_bus = ret;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700350 break;
351
352 case PCI_CLASS_BRIDGE_CARDBUS:
353 /*
354 * just do a minimal setup of the bridge,
355 * let the OS take care of the rest
356 */
Stefan Roesea7a029d2021-01-12 12:03:43 +0100357 dm_pciauto_setup_device(dev, 0, pci_mem, pci_prefetch, pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700358
359 debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
360 PCI_DEV(dm_pci_get_bdf(dev)));
361
362 break;
363
364#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
365 case PCI_CLASS_BRIDGE_OTHER:
366 debug("PCI Autoconfig: Skipping bridge device %d\n",
367 PCI_DEV(dm_pci_get_bdf(dev)));
368 break;
369#endif
Tom Rini68438622021-05-14 21:34:17 -0400370#if defined(CONFIG_ARCH_MPC834X)
Simon Glass5e23b8b2015-11-29 13:17:49 -0700371 case PCI_CLASS_BRIDGE_OTHER:
372 /*
373 * The host/PCI bridge 1 seems broken in 8349 - it presents
374 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
375 * device claiming resources io/mem/irq.. we only allow for
376 * the PIMMR window to be allocated (BAR0 - 1MB size)
377 */
378 debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
379 dm_pciauto_setup_device(dev, 0, hose->pci_mem,
Stefan Roesea7a029d2021-01-12 12:03:43 +0100380 hose->pci_prefetch, hose->pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700381 break;
382#endif
383
384 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
385 debug("PCI AutoConfig: Found PowerPC device\n");
Simon Glassf19345b2016-01-15 05:23:21 -0700386 /* fall through */
Simon Glass5e23b8b2015-11-29 13:17:49 -0700387
388 default:
Stefan Roesea7a029d2021-01-12 12:03:43 +0100389 dm_pciauto_setup_device(dev, 6, pci_mem, pci_prefetch, pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700390 break;
391 }
392
393 return sub_bus;
394}