blob: 08082460eb8631cc051b0d6983979f0951e5c0d1 [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>
Vladimir Oltean7f760842021-09-17 15:11:21 +030015#include "pci_internal.h"
Simon Glass5e23b8b2015-11-29 13:17:49 -070016
17/* 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
20#endif
21
Stefan Roesea7a029d2021-01-12 12:03:43 +010022static void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
23 struct pci_region *mem,
24 struct pci_region *prefetch,
25 struct pci_region *io)
Simon Glass5e23b8b2015-11-29 13:17:49 -070026{
27 u32 bar_response;
28 pci_size_t bar_size;
29 u16 cmdstat = 0;
30 int bar, bar_nr = 0;
31 u8 header_type;
32 int rom_addr;
33 pci_addr_t bar_value;
Bin Meng67967042016-02-17 23:14:47 -080034 struct pci_region *bar_res = NULL;
Simon Glass5e23b8b2015-11-29 13:17:49 -070035 int found_mem64 = 0;
36 u16 class;
37
38 dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
39 cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
40 PCI_COMMAND_MASTER;
41
42 for (bar = PCI_BASE_ADDRESS_0;
43 bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
Simon Glass6a73cf32019-09-25 08:56:16 -060044 int ret = 0;
45
Simon Glass5e23b8b2015-11-29 13:17:49 -070046 /* Tickle the BAR and get the response */
Stefan Roesea7a029d2021-01-12 12:03:43 +010047 dm_pci_write_config32(dev, bar, 0xffffffff);
Simon Glass5e23b8b2015-11-29 13:17:49 -070048 dm_pci_read_config32(dev, bar, &bar_response);
49
Phil Sutterc1b12632021-01-03 23:06:45 +010050 /* If BAR is not implemented (or invalid) go to the next BAR */
51 if (!bar_response || bar_response == 0xffffffff)
Simon Glass5e23b8b2015-11-29 13:17:49 -070052 continue;
53
54 found_mem64 = 0;
55
56 /* Check the BAR type and set our address mask */
57 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
Phil Sutterc1b12632021-01-03 23:06:45 +010058 bar_size = bar_response & PCI_BASE_ADDRESS_IO_MASK;
59 bar_size &= ~(bar_size - 1);
60
Stefan Roesea7a029d2021-01-12 12:03:43 +010061 bar_res = io;
Simon Glass5e23b8b2015-11-29 13:17:49 -070062
63 debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ",
64 bar_nr, (unsigned long long)bar_size);
65 } else {
66 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
67 PCI_BASE_ADDRESS_MEM_TYPE_64) {
68 u32 bar_response_upper;
69 u64 bar64;
70
Stefan Roesea7a029d2021-01-12 12:03:43 +010071 dm_pci_write_config32(dev, bar + 4, 0xffffffff);
Simon Glass5e23b8b2015-11-29 13:17:49 -070072 dm_pci_read_config32(dev, bar + 4,
73 &bar_response_upper);
74
75 bar64 = ((u64)bar_response_upper << 32) |
76 bar_response;
77
78 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK)
79 + 1;
Stefan Roesea7a029d2021-01-12 12:03:43 +010080 found_mem64 = 1;
Simon Glass5e23b8b2015-11-29 13:17:49 -070081 } else {
82 bar_size = (u32)(~(bar_response &
83 PCI_BASE_ADDRESS_MEM_MASK) + 1);
84 }
Stefan Roesea7a029d2021-01-12 12:03:43 +010085
86 if (prefetch &&
87 (bar_response & PCI_BASE_ADDRESS_MEM_PREFETCH))
88 bar_res = prefetch;
89 else
90 bar_res = mem;
Simon Glass5e23b8b2015-11-29 13:17:49 -070091
Phil Suttera62de442021-03-03 01:57:35 +010092 debug("PCI Autoconfig: BAR %d, %s%s, size=0x%llx, ",
Simon Glass5e23b8b2015-11-29 13:17:49 -070093 bar_nr, bar_res == prefetch ? "Prf" : "Mem",
Phil Suttera62de442021-03-03 01:57:35 +010094 found_mem64 ? "64" : "",
Simon Glass5e23b8b2015-11-29 13:17:49 -070095 (unsigned long long)bar_size);
96 }
97
Stefan Roesea7a029d2021-01-12 12:03:43 +010098 ret = pciauto_region_allocate(bar_res, bar_size,
99 &bar_value, found_mem64);
100 if (ret)
101 printf("PCI: Failed autoconfig bar %x\n", bar);
102
103 if (!ret) {
Simon Glass5e23b8b2015-11-29 13:17:49 -0700104 /* Write it out and update our limit */
105 dm_pci_write_config32(dev, bar, (u32)bar_value);
106
107 if (found_mem64) {
108 bar += 4;
109#ifdef CONFIG_SYS_PCI_64BIT
110 dm_pci_write_config32(dev, bar,
111 (u32)(bar_value >> 32));
112#else
113 /*
114 * If we are a 64-bit decoder then increment to
115 * the upper 32 bits of the bar and force it to
116 * locate in the lower 4GB of memory.
117 */
118 dm_pci_write_config32(dev, bar, 0x00000000);
119#endif
120 }
121 }
122
123 cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
124 PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
125
126 debug("\n");
127
128 bar_nr++;
129 }
130
Stefan Roesea7a029d2021-01-12 12:03:43 +0100131 /* Configure the expansion ROM address */
132 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
133 header_type &= 0x7f;
134 if (header_type != PCI_HEADER_TYPE_CARDBUS) {
135 rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
136 PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
137 dm_pci_write_config32(dev, rom_addr, 0xfffffffe);
138 dm_pci_read_config32(dev, rom_addr, &bar_response);
139 if (bar_response) {
140 bar_size = -(bar_response & ~1);
141 debug("PCI Autoconfig: ROM, size=%#x, ",
142 (unsigned int)bar_size);
143 if (pciauto_region_allocate(mem, bar_size, &bar_value,
144 false) == 0) {
145 dm_pci_write_config32(dev, rom_addr, bar_value);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700146 }
Stefan Roesea7a029d2021-01-12 12:03:43 +0100147 cmdstat |= PCI_COMMAND_MEMORY;
148 debug("\n");
Simon Glass5e23b8b2015-11-29 13:17:49 -0700149 }
150 }
151
152 /* PCI_COMMAND_IO must be set for VGA device */
153 dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
154 if (class == PCI_CLASS_DISPLAY_VGA)
155 cmdstat |= PCI_COMMAND_IO;
156
157 dm_pci_write_config16(dev, PCI_COMMAND, cmdstat);
158 dm_pci_write_config8(dev, PCI_CACHE_LINE_SIZE,
159 CONFIG_SYS_PCI_CACHE_LINE_SIZE);
160 dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x80);
161}
162
163void dm_pciauto_prescan_setup_bridge(struct udevice *dev, int sub_bus)
164{
165 struct pci_region *pci_mem;
166 struct pci_region *pci_prefetch;
167 struct pci_region *pci_io;
168 u16 cmdstat, prefechable_64;
Pali Rohár8e85f362021-09-10 13:33:35 +0200169 u8 io_32;
Simon Glass4439bc32016-01-18 20:19:16 -0700170 struct udevice *ctlr = pci_get_controller(dev);
171 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700172
173 pci_mem = ctlr_hose->pci_mem;
174 pci_prefetch = ctlr_hose->pci_prefetch;
175 pci_io = ctlr_hose->pci_io;
176
177 dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
178 dm_pci_read_config16(dev, PCI_PREF_MEMORY_BASE, &prefechable_64);
179 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
Pali Rohár8e85f362021-09-10 13:33:35 +0200180 dm_pci_read_config8(dev, PCI_IO_LIMIT, &io_32);
181 io_32 &= PCI_IO_RANGE_TYPE_MASK;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700182
183 /* Configure bus number registers */
184 dm_pci_write_config8(dev, PCI_PRIMARY_BUS,
Simon Glass8b85dfc2020-12-16 21:20:07 -0700185 PCI_BUS(dm_pci_get_bdf(dev)) - dev_seq(ctlr));
186 dm_pci_write_config8(dev, PCI_SECONDARY_BUS, sub_bus - dev_seq(ctlr));
Simon Glass5e23b8b2015-11-29 13:17:49 -0700187 dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, 0xff);
188
189 if (pci_mem) {
190 /* Round memory allocator to 1MB boundary */
191 pciauto_region_align(pci_mem, 0x100000);
192
193 /*
194 * Set up memory and I/O filter limits, assume 32-bit
195 * I/O space
196 */
197 dm_pci_write_config16(dev, PCI_MEMORY_BASE,
Pali Rohár8e85f362021-09-10 13:33:35 +0200198 ((pci_mem->bus_lower & 0xfff00000) >> 16) &
199 PCI_MEMORY_RANGE_MASK);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700200
201 cmdstat |= PCI_COMMAND_MEMORY;
202 }
203
204 if (pci_prefetch) {
205 /* Round memory allocator to 1MB boundary */
206 pciauto_region_align(pci_prefetch, 0x100000);
207
208 /*
209 * Set up memory and I/O filter limits, assume 32-bit
210 * I/O space
211 */
212 dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE,
Pali Rohár8e85f362021-09-10 13:33:35 +0200213 (((pci_prefetch->bus_lower & 0xfff00000) >> 16) &
214 PCI_PREF_RANGE_MASK) | prefechable_64);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700215 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
216#ifdef CONFIG_SYS_PCI_64BIT
217 dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32,
218 pci_prefetch->bus_lower >> 32);
219#else
220 dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0x0);
221#endif
222
223 cmdstat |= PCI_COMMAND_MEMORY;
224 } else {
225 /* We don't support prefetchable memory for now, so disable */
Pali Rohár8e85f362021-09-10 13:33:35 +0200226 dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE, 0x1000 |
227 prefechable_64);
228 dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, 0x0 |
229 prefechable_64);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700230 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
231 dm_pci_write_config16(dev, PCI_PREF_BASE_UPPER32, 0x0);
232 dm_pci_write_config16(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
233 }
234 }
235
236 if (pci_io) {
237 /* Round I/O allocator to 4KB boundary */
238 pciauto_region_align(pci_io, 0x1000);
239
240 dm_pci_write_config8(dev, PCI_IO_BASE,
Pali Rohár8e85f362021-09-10 13:33:35 +0200241 (((pci_io->bus_lower & 0x0000f000) >> 8) &
242 PCI_IO_RANGE_MASK) | io_32);
243 if (io_32 == PCI_IO_RANGE_TYPE_32)
244 dm_pci_write_config16(dev, PCI_IO_BASE_UPPER16,
Simon Glass5e23b8b2015-11-29 13:17:49 -0700245 (pci_io->bus_lower & 0xffff0000) >> 16);
246
247 cmdstat |= PCI_COMMAND_IO;
248 }
249
250 /* Enable memory and I/O accesses, enable bus master */
251 dm_pci_write_config16(dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
252}
253
254void dm_pciauto_postscan_setup_bridge(struct udevice *dev, int sub_bus)
255{
256 struct pci_region *pci_mem;
257 struct pci_region *pci_prefetch;
258 struct pci_region *pci_io;
Simon Glass4439bc32016-01-18 20:19:16 -0700259 struct udevice *ctlr = pci_get_controller(dev);
260 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700261
262 pci_mem = ctlr_hose->pci_mem;
263 pci_prefetch = ctlr_hose->pci_prefetch;
264 pci_io = ctlr_hose->pci_io;
265
266 /* Configure bus number registers */
Simon Glass8b85dfc2020-12-16 21:20:07 -0700267 dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, sub_bus - dev_seq(ctlr));
Simon Glass5e23b8b2015-11-29 13:17:49 -0700268
269 if (pci_mem) {
270 /* Round memory allocator to 1MB boundary */
271 pciauto_region_align(pci_mem, 0x100000);
272
273 dm_pci_write_config16(dev, PCI_MEMORY_LIMIT,
Pali Rohár8e85f362021-09-10 13:33:35 +0200274 ((pci_mem->bus_lower - 1) >> 16) &
275 PCI_MEMORY_RANGE_MASK);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700276 }
277
278 if (pci_prefetch) {
279 u16 prefechable_64;
280
281 dm_pci_read_config16(dev, PCI_PREF_MEMORY_LIMIT,
282 &prefechable_64);
283 prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
284
285 /* Round memory allocator to 1MB boundary */
286 pciauto_region_align(pci_prefetch, 0x100000);
287
288 dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT,
Pali Rohár8e85f362021-09-10 13:33:35 +0200289 (((pci_prefetch->bus_lower - 1) >> 16) &
290 PCI_PREF_RANGE_MASK) | prefechable_64);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700291 if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
292#ifdef CONFIG_SYS_PCI_64BIT
293 dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32,
294 (pci_prefetch->bus_lower - 1) >> 32);
295#else
296 dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
297#endif
298 }
299
300 if (pci_io) {
Pali Rohár8e85f362021-09-10 13:33:35 +0200301 u8 io_32;
302
303 dm_pci_read_config8(dev, PCI_IO_LIMIT,
304 &io_32);
305 io_32 &= PCI_IO_RANGE_TYPE_MASK;
306
Simon Glass5e23b8b2015-11-29 13:17:49 -0700307 /* Round I/O allocator to 4KB boundary */
308 pciauto_region_align(pci_io, 0x1000);
309
310 dm_pci_write_config8(dev, PCI_IO_LIMIT,
Pali Rohár8e85f362021-09-10 13:33:35 +0200311 ((((pci_io->bus_lower - 1) & 0x0000f000) >> 8) &
312 PCI_IO_RANGE_MASK) | io_32);
313 if (io_32 == PCI_IO_RANGE_TYPE_32)
314 dm_pci_write_config16(dev, PCI_IO_LIMIT_UPPER16,
Simon Glass5e23b8b2015-11-29 13:17:49 -0700315 ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
316 }
317}
318
319/*
320 * HJF: Changed this to return int. I think this is required
321 * to get the correct result when scanning bridges
322 */
323int dm_pciauto_config_device(struct udevice *dev)
324{
325 struct pci_region *pci_mem;
326 struct pci_region *pci_prefetch;
327 struct pci_region *pci_io;
328 unsigned int sub_bus = PCI_BUS(dm_pci_get_bdf(dev));
329 unsigned short class;
Simon Glass4439bc32016-01-18 20:19:16 -0700330 struct udevice *ctlr = pci_get_controller(dev);
331 struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
Simon Glass42f36632020-12-16 21:20:18 -0700332 int ret;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700333
Simon Glass5e23b8b2015-11-29 13:17:49 -0700334 pci_mem = ctlr_hose->pci_mem;
335 pci_prefetch = ctlr_hose->pci_prefetch;
336 pci_io = ctlr_hose->pci_io;
337
338 dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
339
340 switch (class) {
341 case PCI_CLASS_BRIDGE_PCI:
342 debug("PCI Autoconfig: Found P2P bridge, device %d\n",
343 PCI_DEV(dm_pci_get_bdf(dev)));
344
Stefan Roesea7a029d2021-01-12 12:03:43 +0100345 dm_pciauto_setup_device(dev, 2, pci_mem, pci_prefetch, pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700346
Simon Glass42f36632020-12-16 21:20:18 -0700347 ret = dm_pci_hose_probe_bus(dev);
348 if (ret < 0)
349 return log_msg_ret("probe", ret);
350 sub_bus = ret;
Simon Glass5e23b8b2015-11-29 13:17:49 -0700351 break;
352
353 case PCI_CLASS_BRIDGE_CARDBUS:
354 /*
355 * just do a minimal setup of the bridge,
356 * let the OS take care of the rest
357 */
Stefan Roesea7a029d2021-01-12 12:03:43 +0100358 dm_pciauto_setup_device(dev, 0, pci_mem, pci_prefetch, pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700359
360 debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
361 PCI_DEV(dm_pci_get_bdf(dev)));
362
363 break;
364
365#if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
366 case PCI_CLASS_BRIDGE_OTHER:
367 debug("PCI Autoconfig: Skipping bridge device %d\n",
368 PCI_DEV(dm_pci_get_bdf(dev)));
369 break;
370#endif
Tom Rini68438622021-05-14 21:34:17 -0400371#if defined(CONFIG_ARCH_MPC834X)
Simon Glass5e23b8b2015-11-29 13:17:49 -0700372 case PCI_CLASS_BRIDGE_OTHER:
373 /*
374 * The host/PCI bridge 1 seems broken in 8349 - it presents
375 * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
376 * device claiming resources io/mem/irq.. we only allow for
377 * the PIMMR window to be allocated (BAR0 - 1MB size)
378 */
379 debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
380 dm_pciauto_setup_device(dev, 0, hose->pci_mem,
Stefan Roesea7a029d2021-01-12 12:03:43 +0100381 hose->pci_prefetch, hose->pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700382 break;
383#endif
384
385 case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
386 debug("PCI AutoConfig: Found PowerPC device\n");
Simon Glassf19345b2016-01-15 05:23:21 -0700387 /* fall through */
Simon Glass5e23b8b2015-11-29 13:17:49 -0700388
389 default:
Stefan Roesea7a029d2021-01-12 12:03:43 +0100390 dm_pciauto_setup_device(dev, 6, pci_mem, pci_prefetch, pci_io);
Simon Glass5e23b8b2015-11-29 13:17:49 -0700391 break;
392 }
393
394 return sub_bus;
395}