blob: 83fd9a068f3635510c6596dfc45e3debc5c48e42 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
4 *
wdenkf4688a22003-05-28 08:06:31 +00005 * (C) Copyright 2002, 2003
wdenkc6097192002-11-03 00:24:07 +00006 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00009 */
10
11/*
12 * PCI routines
13 */
14
15#include <common.h>
16
wdenkc6097192002-11-03 00:24:07 +000017#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000018#include <asm/processor.h>
19#include <asm/io.h>
20#include <pci.h>
21
Bin Mengf1b81fc2014-12-30 22:53:21 +080022DECLARE_GLOBAL_DATA_PTR;
23
wdenkf4688a22003-05-28 08:06:31 +000024#define PCI_HOSE_OP(rw, size, type) \
Wolfgang Denka1be4762008-05-20 16:00:29 +020025int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
26 pci_dev_t dev, \
wdenkf4688a22003-05-28 08:06:31 +000027 int offset, type value) \
28{ \
29 return hose->rw##_##size(hose, dev, offset, value); \
wdenkc6097192002-11-03 00:24:07 +000030}
31
32PCI_HOSE_OP(read, byte, u8 *)
33PCI_HOSE_OP(read, word, u16 *)
34PCI_HOSE_OP(read, dword, u32 *)
35PCI_HOSE_OP(write, byte, u8)
36PCI_HOSE_OP(write, word, u16)
37PCI_HOSE_OP(write, dword, u32)
38
wdenkf4688a22003-05-28 08:06:31 +000039#define PCI_OP(rw, size, type, error_code) \
40int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
41{ \
42 struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
43 \
44 if (!hose) \
45 { \
46 error_code; \
47 return -1; \
48 } \
49 \
50 return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
wdenkc6097192002-11-03 00:24:07 +000051}
52
53PCI_OP(read, byte, u8 *, *value = 0xff)
54PCI_OP(read, word, u16 *, *value = 0xffff)
55PCI_OP(read, dword, u32 *, *value = 0xffffffff)
56PCI_OP(write, byte, u8, )
57PCI_OP(write, word, u16, )
58PCI_OP(write, dword, u32, )
59
wdenkf4688a22003-05-28 08:06:31 +000060#define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
61int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
Wolfgang Denka1be4762008-05-20 16:00:29 +020062 pci_dev_t dev, \
wdenkf4688a22003-05-28 08:06:31 +000063 int offset, type val) \
64{ \
65 u32 val32; \
66 \
Shinya Kuribayashif19da9d2007-08-17 12:43:44 +090067 if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
68 *val = -1; \
wdenkf4688a22003-05-28 08:06:31 +000069 return -1; \
Shinya Kuribayashif19da9d2007-08-17 12:43:44 +090070 } \
wdenkf4688a22003-05-28 08:06:31 +000071 \
72 *val = (val32 >> ((offset & (int)off_mask) * 8)); \
73 \
74 return 0; \
wdenkc6097192002-11-03 00:24:07 +000075}
76
wdenkf4688a22003-05-28 08:06:31 +000077#define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
78int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
Wolfgang Denka1be4762008-05-20 16:00:29 +020079 pci_dev_t dev, \
wdenkf4688a22003-05-28 08:06:31 +000080 int offset, type val) \
81{ \
wdenk19c8fb72004-04-18 22:26:17 +000082 u32 val32, mask, ldata, shift; \
wdenkf4688a22003-05-28 08:06:31 +000083 \
84 if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
85 return -1; \
86 \
wdenk19c8fb72004-04-18 22:26:17 +000087 shift = ((offset & (int)off_mask) * 8); \
88 ldata = (((unsigned long)val) & val_mask) << shift; \
89 mask = val_mask << shift; \
wdenkf4688a22003-05-28 08:06:31 +000090 val32 = (val32 & ~mask) | ldata; \
91 \
92 if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
93 return -1; \
94 \
95 return 0; \
wdenkc6097192002-11-03 00:24:07 +000096}
97
98PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
99PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
100PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
101PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
102
Becky Bruce0709bfc2009-02-03 18:10:50 -0600103/* Get a virtual address associated with a BAR region */
104void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
105{
106 pci_addr_t pci_bus_addr;
Kumar Gala1873d5c2012-09-19 04:47:36 +0000107 u32 bar_response;
Becky Bruce0709bfc2009-02-03 18:10:50 -0600108
109 /* read BAR address */
110 pci_read_config_dword(pdev, bar, &bar_response);
Kumar Gala1873d5c2012-09-19 04:47:36 +0000111 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
Becky Bruce0709bfc2009-02-03 18:10:50 -0600112
113 /*
114 * Pass "0" as the length argument to pci_bus_to_virt. The arg
115 * isn't actualy used on any platform because u-boot assumes a static
116 * linear mapping. In the future, this could read the BAR size
117 * and pass that as the size if needed.
118 */
119 return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
120}
121
wdenkc6097192002-11-03 00:24:07 +0000122/*
123 *
124 */
125
John Schmoller60e877f2010-10-22 00:20:23 -0500126static struct pci_controller* hose_head;
wdenkc6097192002-11-03 00:24:07 +0000127
Bin Mengf1b81fc2014-12-30 22:53:21 +0800128struct pci_controller *pci_get_hose_head(void)
129{
130 if (gd->hose)
131 return gd->hose;
132
133 return hose_head;
134}
135
wdenkc6097192002-11-03 00:24:07 +0000136void pci_register_hose(struct pci_controller* hose)
137{
138 struct pci_controller **phose = &hose_head;
139
140 while(*phose)
141 phose = &(*phose)->next;
142
143 hose->next = NULL;
144
145 *phose = hose;
146}
147
Andrew Sharp68705132012-08-29 14:16:29 +0000148struct pci_controller *pci_bus_to_hose(int bus)
wdenkc6097192002-11-03 00:24:07 +0000149{
150 struct pci_controller *hose;
151
Bin Mengf1b81fc2014-12-30 22:53:21 +0800152 for (hose = pci_get_hose_head(); hose; hose = hose->next) {
wdenkf4688a22003-05-28 08:06:31 +0000153 if (bus >= hose->first_busno && bus <= hose->last_busno)
wdenkc6097192002-11-03 00:24:07 +0000154 return hose;
Andrew Sharp68705132012-08-29 14:16:29 +0000155 }
wdenkc6097192002-11-03 00:24:07 +0000156
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200157 printf("pci_bus_to_hose() failed\n");
wdenkc6097192002-11-03 00:24:07 +0000158 return NULL;
159}
160
Kumar Galadb943ed2010-12-17 05:57:25 -0600161struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
162{
163 struct pci_controller *hose;
164
Bin Mengf1b81fc2014-12-30 22:53:21 +0800165 for (hose = pci_get_hose_head(); hose; hose = hose->next) {
Kumar Galadb943ed2010-12-17 05:57:25 -0600166 if (hose->cfg_addr == cfg_addr)
167 return hose;
168 }
169
170 return NULL;
171}
172
Anton Vorontsov597b8c42009-02-19 18:20:41 +0300173int pci_last_busno(void)
174{
Bin Mengf1b81fc2014-12-30 22:53:21 +0800175 struct pci_controller *hose = pci_get_hose_head();
Anton Vorontsov597b8c42009-02-19 18:20:41 +0300176
177 if (!hose)
178 return -1;
179
180 while (hose->next)
181 hose = hose->next;
182
183 return hose->last_busno;
184}
185
wdenkc6097192002-11-03 00:24:07 +0000186pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
187{
188 struct pci_controller * hose;
189 u16 vendor, device;
190 u8 header_type;
191 pci_dev_t bdf;
192 int i, bus, found_multi = 0;
193
Bin Mengf1b81fc2014-12-30 22:53:21 +0800194 for (hose = pci_get_hose_head(); hose; hose = hose->next) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200195#ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
wdenkc6097192002-11-03 00:24:07 +0000196 for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
197#else
198 for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
199#endif
Andrew Sharp68705132012-08-29 14:16:29 +0000200 for (bdf = PCI_BDF(bus, 0, 0);
Heiko Schocherd7f77fb2006-06-19 11:02:41 +0200201#if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
Andrew Sharp68705132012-08-29 14:16:29 +0000202 bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
203 PCI_MAX_PCI_FUNCTIONS - 1);
wdenkc6097192002-11-03 00:24:07 +0000204#else
Andrew Sharp68705132012-08-29 14:16:29 +0000205 bdf < PCI_BDF(bus + 1, 0, 0);
wdenkc6097192002-11-03 00:24:07 +0000206#endif
Andrew Sharp68705132012-08-29 14:16:29 +0000207 bdf += PCI_BDF(0, 0, 1)) {
Thierry Reding699b6d32014-11-12 18:26:49 -0700208 if (pci_skip_dev(hose, bdf))
209 continue;
210
wdenkf4688a22003-05-28 08:06:31 +0000211 if (!PCI_FUNC(bdf)) {
wdenkc6097192002-11-03 00:24:07 +0000212 pci_read_config_byte(bdf,
213 PCI_HEADER_TYPE,
214 &header_type);
215
216 found_multi = header_type & 0x80;
wdenkf4688a22003-05-28 08:06:31 +0000217 } else {
wdenkc6097192002-11-03 00:24:07 +0000218 if (!found_multi)
219 continue;
220 }
221
222 pci_read_config_word(bdf,
223 PCI_VENDOR_ID,
224 &vendor);
225 pci_read_config_word(bdf,
226 PCI_DEVICE_ID,
227 &device);
228
Andrew Sharp68705132012-08-29 14:16:29 +0000229 for (i = 0; ids[i].vendor != 0; i++) {
wdenkc6097192002-11-03 00:24:07 +0000230 if (vendor == ids[i].vendor &&
Andrew Sharp68705132012-08-29 14:16:29 +0000231 device == ids[i].device) {
wdenkc6097192002-11-03 00:24:07 +0000232 if (index <= 0)
233 return bdf;
234
235 index--;
236 }
Andrew Sharp68705132012-08-29 14:16:29 +0000237 }
wdenkc6097192002-11-03 00:24:07 +0000238 }
239 }
240
Andrew Sharp68705132012-08-29 14:16:29 +0000241 return -1;
wdenkc6097192002-11-03 00:24:07 +0000242}
243
244pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
245{
Bin Mengf1b81fc2014-12-30 22:53:21 +0800246 struct pci_device_id ids[2] = { {}, {0, 0} };
wdenkc6097192002-11-03 00:24:07 +0000247
248 ids[0].vendor = vendor;
249 ids[0].device = device;
250
251 return pci_find_devices(ids, index);
252}
253
254/*
255 *
256 */
257
Andrew Sharp68705132012-08-29 14:16:29 +0000258int __pci_hose_phys_to_bus(struct pci_controller *hose,
Kumar Galadeb55f22009-02-06 09:49:32 -0600259 phys_addr_t phys_addr,
260 unsigned long flags,
261 unsigned long skip_mask,
262 pci_addr_t *ba)
wdenkc6097192002-11-03 00:24:07 +0000263{
264 struct pci_region *res;
Kumar Galaad714f52008-10-21 08:36:08 -0500265 pci_addr_t bus_addr;
wdenkc6097192002-11-03 00:24:07 +0000266 int i;
267
wdenkf4688a22003-05-28 08:06:31 +0000268 for (i = 0; i < hose->region_count; i++) {
wdenkc6097192002-11-03 00:24:07 +0000269 res = &hose->regions[i];
270
271 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
272 continue;
273
Kumar Galadeb55f22009-02-06 09:49:32 -0600274 if (res->flags & skip_mask)
275 continue;
276
wdenkc6097192002-11-03 00:24:07 +0000277 bus_addr = phys_addr - res->phys_start + res->bus_start;
278
279 if (bus_addr >= res->bus_start &&
wdenkf4688a22003-05-28 08:06:31 +0000280 bus_addr < res->bus_start + res->size) {
Kumar Galadeb55f22009-02-06 09:49:32 -0600281 *ba = bus_addr;
282 return 0;
wdenkc6097192002-11-03 00:24:07 +0000283 }
284 }
285
Kumar Galadeb55f22009-02-06 09:49:32 -0600286 return 1;
wdenkc6097192002-11-03 00:24:07 +0000287}
288
Kumar Galadeb55f22009-02-06 09:49:32 -0600289pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
290 phys_addr_t phys_addr,
291 unsigned long flags)
wdenkc6097192002-11-03 00:24:07 +0000292{
Kumar Galadeb55f22009-02-06 09:49:32 -0600293 pci_addr_t bus_addr = 0;
294 int ret;
wdenkc6097192002-11-03 00:24:07 +0000295
wdenkf4688a22003-05-28 08:06:31 +0000296 if (!hose) {
Andrew Sharp68705132012-08-29 14:16:29 +0000297 puts("pci_hose_phys_to_bus: invalid hose\n");
Kumar Galadeb55f22009-02-06 09:49:32 -0600298 return bus_addr;
wdenkc6097192002-11-03 00:24:07 +0000299 }
300
Andrew Sharp68705132012-08-29 14:16:29 +0000301 /*
302 * if PCI_REGION_MEM is set we do a two pass search with preference
303 * on matches that don't have PCI_REGION_SYS_MEMORY set
304 */
Kumar Galadeb55f22009-02-06 09:49:32 -0600305 if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
306 ret = __pci_hose_phys_to_bus(hose, phys_addr,
307 flags, PCI_REGION_SYS_MEMORY, &bus_addr);
308 if (!ret)
309 return bus_addr;
310 }
311
312 ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
313
314 if (ret)
Andrew Sharp68705132012-08-29 14:16:29 +0000315 puts("pci_hose_phys_to_bus: invalid physical address\n");
Kumar Galadeb55f22009-02-06 09:49:32 -0600316
317 return bus_addr;
318}
319
Andrew Sharp68705132012-08-29 14:16:29 +0000320int __pci_hose_bus_to_phys(struct pci_controller *hose,
Kumar Galadeb55f22009-02-06 09:49:32 -0600321 pci_addr_t bus_addr,
322 unsigned long flags,
323 unsigned long skip_mask,
324 phys_addr_t *pa)
325{
326 struct pci_region *res;
327 int i;
328
wdenkf4688a22003-05-28 08:06:31 +0000329 for (i = 0; i < hose->region_count; i++) {
wdenkc6097192002-11-03 00:24:07 +0000330 res = &hose->regions[i];
331
332 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
333 continue;
334
Kumar Galadeb55f22009-02-06 09:49:32 -0600335 if (res->flags & skip_mask)
336 continue;
337
wdenkc6097192002-11-03 00:24:07 +0000338 if (bus_addr >= res->bus_start &&
Stephen Warren739a14c2014-08-11 16:09:28 -0600339 (bus_addr - res->bus_start) < res->size) {
Kumar Galadeb55f22009-02-06 09:49:32 -0600340 *pa = (bus_addr - res->bus_start + res->phys_start);
341 return 0;
wdenkc6097192002-11-03 00:24:07 +0000342 }
343 }
344
Kumar Galadeb55f22009-02-06 09:49:32 -0600345 return 1;
346}
347
348phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
349 pci_addr_t bus_addr,
350 unsigned long flags)
351{
352 phys_addr_t phys_addr = 0;
353 int ret;
354
355 if (!hose) {
Andrew Sharp68705132012-08-29 14:16:29 +0000356 puts("pci_hose_bus_to_phys: invalid hose\n");
Kumar Galadeb55f22009-02-06 09:49:32 -0600357 return phys_addr;
358 }
359
Andrew Sharp68705132012-08-29 14:16:29 +0000360 /*
361 * if PCI_REGION_MEM is set we do a two pass search with preference
362 * on matches that don't have PCI_REGION_SYS_MEMORY set
363 */
Kumar Galadeb55f22009-02-06 09:49:32 -0600364 if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
365 ret = __pci_hose_bus_to_phys(hose, bus_addr,
366 flags, PCI_REGION_SYS_MEMORY, &phys_addr);
367 if (!ret)
368 return phys_addr;
369 }
370
371 ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
372
373 if (ret)
Andrew Sharp68705132012-08-29 14:16:29 +0000374 puts("pci_hose_bus_to_phys: invalid physical address\n");
wdenkc6097192002-11-03 00:24:07 +0000375
Kumar Galadeb55f22009-02-06 09:49:32 -0600376 return phys_addr;
wdenkc6097192002-11-03 00:24:07 +0000377}
378
Simon Glass6ac5af42014-11-14 18:18:30 -0700379void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
380 u32 addr_and_ctrl)
381{
382 int bar;
383
384 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
385 pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
386}
387
388u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
389{
390 u32 addr;
391 int bar;
392
393 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
394 pci_hose_read_config_dword(hose, dev, bar, &addr);
395 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
396 return addr & PCI_BASE_ADDRESS_IO_MASK;
397 else
398 return addr & PCI_BASE_ADDRESS_MEM_MASK;
399}
wdenkc6097192002-11-03 00:24:07 +0000400
401int pci_hose_config_device(struct pci_controller *hose,
402 pci_dev_t dev,
403 unsigned long io,
Kumar Galaad714f52008-10-21 08:36:08 -0500404 pci_addr_t mem,
wdenkc6097192002-11-03 00:24:07 +0000405 unsigned long command)
406{
Kumar Gala1873d5c2012-09-19 04:47:36 +0000407 u32 bar_response;
Andrew Sharpf4f24822012-08-01 12:27:16 +0000408 unsigned int old_command;
Kumar Galaad714f52008-10-21 08:36:08 -0500409 pci_addr_t bar_value;
410 pci_size_t bar_size;
wdenkc6097192002-11-03 00:24:07 +0000411 unsigned char pin;
412 int bar, found_mem64;
413
Andrew Sharp68705132012-08-29 14:16:29 +0000414 debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
415 (u64)mem, command);
wdenkc6097192002-11-03 00:24:07 +0000416
Andrew Sharp68705132012-08-29 14:16:29 +0000417 pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
wdenkc6097192002-11-03 00:24:07 +0000418
Wolfgang Denk03bebef2010-03-09 14:27:25 +0100419 for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
Andrew Sharp68705132012-08-29 14:16:29 +0000420 pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
421 pci_hose_read_config_dword(hose, dev, bar, &bar_response);
wdenkc6097192002-11-03 00:24:07 +0000422
423 if (!bar_response)
424 continue;
425
426 found_mem64 = 0;
427
428 /* Check the BAR type and set our address mask */
wdenkf4688a22003-05-28 08:06:31 +0000429 if (bar_response & PCI_BASE_ADDRESS_SPACE) {
wdenkc6097192002-11-03 00:24:07 +0000430 bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
wdenkf4688a22003-05-28 08:06:31 +0000431 /* round up region base address to a multiple of size */
wdenkc6097192002-11-03 00:24:07 +0000432 io = ((io - 1) | (bar_size - 1)) + 1;
wdenkf4688a22003-05-28 08:06:31 +0000433 bar_value = io;
434 /* compute new region base address */
435 io = io + bar_size;
436 } else {
437 if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
Kumar Galaad714f52008-10-21 08:36:08 -0500438 PCI_BASE_ADDRESS_MEM_TYPE_64) {
439 u32 bar_response_upper;
440 u64 bar64;
Andrew Sharp68705132012-08-29 14:16:29 +0000441 pci_hose_write_config_dword(hose, dev, bar + 4,
442 0xffffffff);
443 pci_hose_read_config_dword(hose, dev, bar + 4,
444 &bar_response_upper);
wdenkc6097192002-11-03 00:24:07 +0000445
Kumar Galaad714f52008-10-21 08:36:08 -0500446 bar64 = ((u64)bar_response_upper << 32) | bar_response;
447
448 bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
449 found_mem64 = 1;
450 } else {
451 bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
452 }
wdenkc6097192002-11-03 00:24:07 +0000453
wdenkf4688a22003-05-28 08:06:31 +0000454 /* round up region base address to multiple of size */
wdenkc6097192002-11-03 00:24:07 +0000455 mem = ((mem - 1) | (bar_size - 1)) + 1;
wdenkf4688a22003-05-28 08:06:31 +0000456 bar_value = mem;
457 /* compute new region base address */
458 mem = mem + bar_size;
wdenkc6097192002-11-03 00:24:07 +0000459 }
460
461 /* Write it out and update our limit */
Kumar Galaad714f52008-10-21 08:36:08 -0500462 pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
wdenkc6097192002-11-03 00:24:07 +0000463
wdenkf4688a22003-05-28 08:06:31 +0000464 if (found_mem64) {
wdenkc6097192002-11-03 00:24:07 +0000465 bar += 4;
Kumar Galaad714f52008-10-21 08:36:08 -0500466#ifdef CONFIG_SYS_PCI_64BIT
Andrew Sharp68705132012-08-29 14:16:29 +0000467 pci_hose_write_config_dword(hose, dev, bar,
468 (u32)(bar_value >> 32));
Kumar Galaad714f52008-10-21 08:36:08 -0500469#else
Andrew Sharp68705132012-08-29 14:16:29 +0000470 pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
Kumar Galaad714f52008-10-21 08:36:08 -0500471#endif
wdenkc6097192002-11-03 00:24:07 +0000472 }
473 }
474
475 /* Configure Cache Line Size Register */
Andrew Sharp68705132012-08-29 14:16:29 +0000476 pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
wdenkc6097192002-11-03 00:24:07 +0000477
478 /* Configure Latency Timer */
Andrew Sharp68705132012-08-29 14:16:29 +0000479 pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
wdenkc6097192002-11-03 00:24:07 +0000480
481 /* Disable interrupt line, if device says it wants to use interrupts */
Andrew Sharp68705132012-08-29 14:16:29 +0000482 pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
wdenkf4688a22003-05-28 08:06:31 +0000483 if (pin != 0) {
Andrew Sharp68705132012-08-29 14:16:29 +0000484 pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff);
wdenkc6097192002-11-03 00:24:07 +0000485 }
486
Andrew Sharp68705132012-08-29 14:16:29 +0000487 pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
488 pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
wdenkf4688a22003-05-28 08:06:31 +0000489 (old_command & 0xffff0000) | command);
wdenkc6097192002-11-03 00:24:07 +0000490
491 return 0;
492}
493
494/*
495 *
496 */
497
498struct pci_config_table *pci_find_config(struct pci_controller *hose,
499 unsigned short class,
500 unsigned int vendor,
501 unsigned int device,
502 unsigned int bus,
503 unsigned int dev,
504 unsigned int func)
505{
506 struct pci_config_table *table;
507
wdenkf4688a22003-05-28 08:06:31 +0000508 for (table = hose->config_table; table && table->vendor; table++) {
wdenkc6097192002-11-03 00:24:07 +0000509 if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
510 (table->device == PCI_ANY_ID || table->device == device) &&
511 (table->class == PCI_ANY_ID || table->class == class) &&
512 (table->bus == PCI_ANY_ID || table->bus == bus) &&
513 (table->dev == PCI_ANY_ID || table->dev == dev) &&
wdenkf4688a22003-05-28 08:06:31 +0000514 (table->func == PCI_ANY_ID || table->func == func)) {
wdenkc6097192002-11-03 00:24:07 +0000515 return table;
516 }
517 }
518
519 return NULL;
520}
521
522void pci_cfgfunc_config_device(struct pci_controller *hose,
523 pci_dev_t dev,
524 struct pci_config_table *entry)
525{
Andrew Sharp68705132012-08-29 14:16:29 +0000526 pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
527 entry->priv[2]);
wdenkc6097192002-11-03 00:24:07 +0000528}
529
530void pci_cfgfunc_do_nothing(struct pci_controller *hose,
531 pci_dev_t dev, struct pci_config_table *entry)
532{
533}
534
535/*
Andrew Sharp68705132012-08-29 14:16:29 +0000536 * HJF: Changed this to return int. I think this is required
wdenk452cfd62002-11-19 11:04:11 +0000537 * to get the correct result when scanning bridges
538 */
539extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
wdenkc6097192002-11-03 00:24:07 +0000540
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500541#if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW)
542const char * pci_class_str(u8 class)
543{
544 switch (class) {
545 case PCI_CLASS_NOT_DEFINED:
546 return "Build before PCI Rev2.0";
547 break;
548 case PCI_BASE_CLASS_STORAGE:
549 return "Mass storage controller";
550 break;
551 case PCI_BASE_CLASS_NETWORK:
552 return "Network controller";
553 break;
554 case PCI_BASE_CLASS_DISPLAY:
555 return "Display controller";
556 break;
557 case PCI_BASE_CLASS_MULTIMEDIA:
558 return "Multimedia device";
559 break;
560 case PCI_BASE_CLASS_MEMORY:
561 return "Memory controller";
562 break;
563 case PCI_BASE_CLASS_BRIDGE:
564 return "Bridge device";
565 break;
566 case PCI_BASE_CLASS_COMMUNICATION:
567 return "Simple comm. controller";
568 break;
569 case PCI_BASE_CLASS_SYSTEM:
570 return "Base system peripheral";
571 break;
572 case PCI_BASE_CLASS_INPUT:
573 return "Input device";
574 break;
575 case PCI_BASE_CLASS_DOCKING:
576 return "Docking station";
577 break;
578 case PCI_BASE_CLASS_PROCESSOR:
579 return "Processor";
580 break;
581 case PCI_BASE_CLASS_SERIAL:
582 return "Serial bus controller";
583 break;
584 case PCI_BASE_CLASS_INTELLIGENT:
585 return "Intelligent controller";
586 break;
587 case PCI_BASE_CLASS_SATELLITE:
588 return "Satellite controller";
589 break;
590 case PCI_BASE_CLASS_CRYPT:
591 return "Cryptographic device";
592 break;
593 case PCI_BASE_CLASS_SIGNAL_PROCESSING:
594 return "DSP";
595 break;
596 case PCI_CLASS_OTHERS:
597 return "Does not fit any class";
598 break;
599 default:
600 return "???";
601 break;
602 };
603}
604#endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */
605
Jeroen Hofsteeab1bca52014-10-08 22:57:27 +0200606__weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
Stefan Roese41e846f2008-07-08 12:01:47 +0200607{
608 /*
609 * Check if pci device should be skipped in configuration
610 */
611 if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
612#if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
613 /*
614 * Only skip configuration if "pciconfighost" is not set
615 */
616 if (getenv("pciconfighost") == NULL)
617 return 1;
618#else
619 return 1;
620#endif
621 }
622
623 return 0;
624}
Stefan Roese41e846f2008-07-08 12:01:47 +0200625
626#ifdef CONFIG_PCI_SCAN_SHOW
Jeroen Hofsteeab1bca52014-10-08 22:57:27 +0200627__weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
Stefan Roese41e846f2008-07-08 12:01:47 +0200628{
629 if (dev == PCI_BDF(hose->first_busno, 0, 0))
630 return 0;
631
632 return 1;
633}
Stefan Roese41e846f2008-07-08 12:01:47 +0200634#endif /* CONFIG_PCI_SCAN_SHOW */
635
wdenkc6097192002-11-03 00:24:07 +0000636int pci_hose_scan_bus(struct pci_controller *hose, int bus)
637{
Andrew Sharp68705132012-08-29 14:16:29 +0000638 unsigned int sub_bus, found_multi = 0;
wdenkc6097192002-11-03 00:24:07 +0000639 unsigned short vendor, device, class;
640 unsigned char header_type;
Andrew Sharp0e2dc472012-08-29 14:16:30 +0000641#ifndef CONFIG_PCI_PNP
wdenkc6097192002-11-03 00:24:07 +0000642 struct pci_config_table *cfg;
Andrew Sharp0e2dc472012-08-29 14:16:30 +0000643#endif
wdenkc6097192002-11-03 00:24:07 +0000644 pci_dev_t dev;
Peter Tyserb46513d2010-10-29 17:59:29 -0500645#ifdef CONFIG_PCI_SCAN_SHOW
646 static int indent = 0;
647#endif
wdenkc6097192002-11-03 00:24:07 +0000648
649 sub_bus = bus;
650
651 for (dev = PCI_BDF(bus,0,0);
Andrew Sharp68705132012-08-29 14:16:29 +0000652 dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
653 PCI_MAX_PCI_FUNCTIONS - 1);
654 dev += PCI_BDF(0, 0, 1)) {
Stefan Roese41e846f2008-07-08 12:01:47 +0200655
656 if (pci_skip_dev(hose, dev))
657 continue;
wdenkc6097192002-11-03 00:24:07 +0000658
659 if (PCI_FUNC(dev) && !found_multi)
660 continue;
661
662 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
663
664 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
665
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500666 if (vendor == 0xffff || vendor == 0x0000)
667 continue;
wdenkc6097192002-11-03 00:24:07 +0000668
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500669 if (!PCI_FUNC(dev))
670 found_multi = header_type & 0x80;
wdenkc6097192002-11-03 00:24:07 +0000671
Andrew Sharp68705132012-08-29 14:16:29 +0000672 debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
673 PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
wdenkc6097192002-11-03 00:24:07 +0000674
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500675 pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
676 pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
wdenkc6097192002-11-03 00:24:07 +0000677
Tim Harvey231c0762014-08-07 22:49:56 -0700678#ifdef CONFIG_PCI_FIXUP_DEV
679 board_pci_fixup_dev(hose, dev, vendor, device, class);
680#endif
681
Peter Tyserd40cdc42010-10-29 17:59:28 -0500682#ifdef CONFIG_PCI_SCAN_SHOW
Peter Tyserb46513d2010-10-29 17:59:29 -0500683 indent++;
684
685 /* Print leading space, including bus indentation */
686 printf("%*c", indent + 1, ' ');
687
Peter Tyserd40cdc42010-10-29 17:59:28 -0500688 if (pci_print_dev(hose, dev)) {
Peter Tyserb46513d2010-10-29 17:59:29 -0500689 printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
690 PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
Peter Tyserd40cdc42010-10-29 17:59:28 -0500691 vendor, device, pci_class_str(class >> 8));
692 }
693#endif
694
Andrew Sharp0e2dc472012-08-29 14:16:30 +0000695#ifdef CONFIG_PCI_PNP
Masahiro Yamadadb204642014-11-07 03:03:31 +0900696 sub_bus = max((unsigned int)pciauto_config_device(hose, dev),
697 sub_bus);
Andrew Sharp0e2dc472012-08-29 14:16:30 +0000698#else
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500699 cfg = pci_find_config(hose, class, vendor, device,
700 PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
701 if (cfg) {
702 cfg->config_device(hose, dev, cfg);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900703 sub_bus = max(sub_bus,
704 (unsigned int)hose->current_busno);
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500705 }
Andrew Sharp0e2dc472012-08-29 14:16:30 +0000706#endif
Peter Tyserd40cdc42010-10-29 17:59:28 -0500707
Peter Tyserb46513d2010-10-29 17:59:29 -0500708#ifdef CONFIG_PCI_SCAN_SHOW
709 indent--;
710#endif
711
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500712 if (hose->fixup_irq)
713 hose->fixup_irq(hose, dev);
wdenkc6097192002-11-03 00:24:07 +0000714 }
715
716 return sub_bus;
717}
718
719int pci_hose_scan(struct pci_controller *hose)
720{
Anatolij Gustschin2bd2cb12011-10-11 22:44:30 +0000721#if defined(CONFIG_PCI_BOOTDELAY)
Anatolij Gustschin2bd2cb12011-10-11 22:44:30 +0000722 char *s;
723 int i;
724
Bin Mengf1b81fc2014-12-30 22:53:21 +0800725 if (!gd->pcidelay_done) {
Anatolij Gustschin2bd2cb12011-10-11 22:44:30 +0000726 /* wait "pcidelay" ms (if defined)... */
727 s = getenv("pcidelay");
728 if (s) {
729 int val = simple_strtoul(s, NULL, 10);
730 for (i = 0; i < val; i++)
731 udelay(1000);
732 }
Bin Mengf1b81fc2014-12-30 22:53:21 +0800733 gd->pcidelay_done = 1;
Anatolij Gustschin2bd2cb12011-10-11 22:44:30 +0000734 }
735#endif /* CONFIG_PCI_BOOTDELAY */
736
Andrew Sharp68705132012-08-29 14:16:29 +0000737 /*
738 * Start scan at current_busno.
Ed Swarthoutf0d43472007-07-11 14:51:35 -0500739 * PCIe will start scan at first_busno+1.
740 */
Andrew Sharp68705132012-08-29 14:16:29 +0000741 /* For legacy support, ensure current >= first */
Ed Swarthoutf0d43472007-07-11 14:51:35 -0500742 if (hose->first_busno > hose->current_busno)
743 hose->current_busno = hose->first_busno;
wdenkc6097192002-11-03 00:24:07 +0000744#ifdef CONFIG_PCI_PNP
745 pciauto_config_init(hose);
746#endif
Ed Swarthoutf0d43472007-07-11 14:51:35 -0500747 return pci_hose_scan_bus(hose, hose->current_busno);
wdenkc6097192002-11-03 00:24:07 +0000748}
749
stroesef5dd4102003-02-14 11:21:23 +0000750void pci_init(void)
751{
John Schmoller60e877f2010-10-22 00:20:23 -0500752 hose_head = NULL;
753
stroesef5dd4102003-02-14 11:21:23 +0000754 /* now call board specific pci_init()... */
755 pci_init_board();
756}
Zhao Qiang5d39f742013-10-12 13:46:33 +0800757
758/* Returns the address of the requested capability structure within the
759 * device's PCI configuration space or 0 in case the device does not
760 * support it.
761 * */
762int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
763 int cap)
764{
765 int pos;
766 u8 hdr_type;
767
768 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type);
769
770 pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F);
771
772 if (pos)
773 pos = pci_find_cap(hose, dev, pos, cap);
774
775 return pos;
776}
777
778/* Find the header pointer to the Capabilities*/
779int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
780 u8 hdr_type)
781{
782 u16 status;
783
784 pci_hose_read_config_word(hose, dev, PCI_STATUS, &status);
785
786 if (!(status & PCI_STATUS_CAP_LIST))
787 return 0;
788
789 switch (hdr_type) {
790 case PCI_HEADER_TYPE_NORMAL:
791 case PCI_HEADER_TYPE_BRIDGE:
792 return PCI_CAPABILITY_LIST;
793 case PCI_HEADER_TYPE_CARDBUS:
794 return PCI_CB_CAPABILITY_LIST;
795 default:
796 return 0;
797 }
798}
799
800int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap)
801{
802 int ttl = PCI_FIND_CAP_TTL;
803 u8 id;
804 u8 next_pos;
805
806 while (ttl--) {
807 pci_hose_read_config_byte(hose, dev, pos, &next_pos);
808 if (next_pos < CAP_START_POS)
809 break;
810 next_pos &= ~3;
811 pos = (int) next_pos;
812 pci_hose_read_config_byte(hose, dev,
813 pos + PCI_CAP_LIST_ID, &id);
814 if (id == 0xff)
815 break;
816 if (id == cap)
817 return pos;
818 pos += PCI_CAP_LIST_NEXT;
819 }
820 return 0;
821}