blob: 8a992ed82339f61bec2b872054078096a80fd9c3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd188b182014-11-12 22:42:11 -07002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 * (C) Copyright 2008,2009
5 * Graeme Russ, <graeme.russ@gmail.com>
6 *
7 * (C) Copyright 2002
8 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
Simon Glassd188b182014-11-12 22:42:11 -07009 */
10
11#include <common.h>
Simon Glassa219dae2015-03-05 12:25:31 -070012#include <dm.h>
Simon Glass7430f102014-11-12 22:42:12 -070013#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass7430f102014-11-12 22:42:12 -070015#include <malloc.h>
Simon Glassd188b182014-11-12 22:42:11 -070016#include <pci.h>
Simon Glassa219dae2015-03-05 12:25:31 -070017#include <asm/io.h>
Simon Glassd188b182014-11-12 22:42:11 -070018#include <asm/pci.h>
19
Simon Glassa827ba92019-08-31 21:23:18 -060020int pci_x86_read_config(pci_dev_t bdf, uint offset, ulong *valuep,
21 enum pci_size_t size)
Simon Glassa219dae2015-03-05 12:25:31 -070022{
Pali Rohár75987592021-11-26 11:42:50 +010023 outl(PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset), PCI_REG_ADDR);
Simon Glassa219dae2015-03-05 12:25:31 -070024 switch (size) {
25 case PCI_SIZE_8:
26 *valuep = inb(PCI_REG_DATA + (offset & 3));
27 break;
28 case PCI_SIZE_16:
29 *valuep = inw(PCI_REG_DATA + (offset & 2));
30 break;
31 case PCI_SIZE_32:
32 *valuep = inl(PCI_REG_DATA);
33 break;
34 }
35
36 return 0;
37}
38
Simon Glassa827ba92019-08-31 21:23:18 -060039int pci_x86_write_config(pci_dev_t bdf, uint offset, ulong value,
40 enum pci_size_t size)
Simon Glassa219dae2015-03-05 12:25:31 -070041{
Pali Rohár75987592021-11-26 11:42:50 +010042 outl(PCI_CONF1_ADDRESS(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset), PCI_REG_ADDR);
Simon Glassa219dae2015-03-05 12:25:31 -070043 switch (size) {
44 case PCI_SIZE_8:
45 outb(value, PCI_REG_DATA + (offset & 3));
46 break;
47 case PCI_SIZE_16:
48 outw(value, PCI_REG_DATA + (offset & 2));
49 break;
50 case PCI_SIZE_32:
51 outl(value, PCI_REG_DATA);
52 break;
53 }
54
55 return 0;
56}
Bin Menge3e7fa22015-04-24 18:10:03 +080057
Simon Glassa827ba92019-08-31 21:23:18 -060058int pci_x86_clrset_config(pci_dev_t bdf, uint offset, ulong clr, ulong set,
59 enum pci_size_t size)
Simon Glasse46d00c2019-09-25 08:11:37 -060060{
61 ulong value;
62 int ret;
63
Simon Glassa827ba92019-08-31 21:23:18 -060064 ret = pci_x86_read_config(bdf, offset, &value, size);
Simon Glasse46d00c2019-09-25 08:11:37 -060065 if (ret)
66 return ret;
67 value &= ~clr;
68 value |= set;
69
Simon Glassa827ba92019-08-31 21:23:18 -060070 return pci_x86_write_config(bdf, offset, value, size);
Simon Glasse46d00c2019-09-25 08:11:37 -060071}
72
Bin Meng31a2dc62015-07-15 16:23:40 +080073void pci_assign_irqs(int bus, int device, u8 irq[4])
Bin Menge3e7fa22015-04-24 18:10:03 +080074{
75 pci_dev_t bdf;
Bin Meng31a2dc62015-07-15 16:23:40 +080076 int func;
77 u16 vendor;
Bin Menge3e7fa22015-04-24 18:10:03 +080078 u8 pin, line;
79
Bin Meng31a2dc62015-07-15 16:23:40 +080080 for (func = 0; func < 8; func++) {
81 bdf = PCI_BDF(bus, device, func);
Bin Meng58316f92016-02-01 01:40:57 -080082 pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
Bin Meng31a2dc62015-07-15 16:23:40 +080083 if (vendor == 0xffff || vendor == 0x0000)
84 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080085
Bin Meng58316f92016-02-01 01:40:57 -080086 pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
Bin Menge3e7fa22015-04-24 18:10:03 +080087
Bin Meng31a2dc62015-07-15 16:23:40 +080088 /* PCI spec says all values except 1..4 are reserved */
89 if ((pin < 1) || (pin > 4))
90 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080091
Bin Meng31a2dc62015-07-15 16:23:40 +080092 line = irq[pin - 1];
Bin Meng6fc0e8a2015-07-15 16:23:41 +080093 if (!line)
94 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080095
Bin Meng31a2dc62015-07-15 16:23:40 +080096 debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
97 line, bus, device, func, 'A' + pin - 1);
Bin Menge3e7fa22015-04-24 18:10:03 +080098
Bin Meng58316f92016-02-01 01:40:57 -080099 pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
Bin Meng31a2dc62015-07-15 16:23:40 +0800100 }
Bin Menge3e7fa22015-04-24 18:10:03 +0800101}