blob: c6218250e161f12e65d190daa3f5b9f3344b6e3e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass76805502014-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 Glass76805502014-11-12 22:42:11 -07009 */
10
11#include <common.h>
Simon Glass4e037812015-03-05 12:25:31 -070012#include <dm.h>
Simon Glassa54d9812014-11-12 22:42:12 -070013#include <errno.h>
14#include <malloc.h>
Simon Glass76805502014-11-12 22:42:11 -070015#include <pci.h>
Simon Glass4e037812015-03-05 12:25:31 -070016#include <asm/io.h>
Simon Glass76805502014-11-12 22:42:11 -070017#include <asm/pci.h>
18
Simon Glass4e037812015-03-05 12:25:31 -070019int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
20 ulong *valuep, enum pci_size_t size)
21{
22 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
23 switch (size) {
24 case PCI_SIZE_8:
25 *valuep = inb(PCI_REG_DATA + (offset & 3));
26 break;
27 case PCI_SIZE_16:
28 *valuep = inw(PCI_REG_DATA + (offset & 2));
29 break;
30 case PCI_SIZE_32:
31 *valuep = inl(PCI_REG_DATA);
32 break;
33 }
34
35 return 0;
36}
37
38int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
39 ulong value, enum pci_size_t size)
40{
41 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
42 switch (size) {
43 case PCI_SIZE_8:
44 outb(value, PCI_REG_DATA + (offset & 3));
45 break;
46 case PCI_SIZE_16:
47 outw(value, PCI_REG_DATA + (offset & 2));
48 break;
49 case PCI_SIZE_32:
50 outl(value, PCI_REG_DATA);
51 break;
52 }
53
54 return 0;
55}
Bin Meng363849b2015-04-24 18:10:03 +080056
Bin Mengda5d4632015-07-15 16:23:40 +080057void pci_assign_irqs(int bus, int device, u8 irq[4])
Bin Meng363849b2015-04-24 18:10:03 +080058{
59 pci_dev_t bdf;
Bin Mengda5d4632015-07-15 16:23:40 +080060 int func;
61 u16 vendor;
Bin Meng363849b2015-04-24 18:10:03 +080062 u8 pin, line;
63
Bin Mengda5d4632015-07-15 16:23:40 +080064 for (func = 0; func < 8; func++) {
65 bdf = PCI_BDF(bus, device, func);
Bin Meng8df01812016-02-01 01:40:57 -080066 pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
Bin Mengda5d4632015-07-15 16:23:40 +080067 if (vendor == 0xffff || vendor == 0x0000)
68 continue;
Bin Meng363849b2015-04-24 18:10:03 +080069
Bin Meng8df01812016-02-01 01:40:57 -080070 pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
Bin Meng363849b2015-04-24 18:10:03 +080071
Bin Mengda5d4632015-07-15 16:23:40 +080072 /* PCI spec says all values except 1..4 are reserved */
73 if ((pin < 1) || (pin > 4))
74 continue;
Bin Meng363849b2015-04-24 18:10:03 +080075
Bin Mengda5d4632015-07-15 16:23:40 +080076 line = irq[pin - 1];
Bin Menge0a5fd92015-07-15 16:23:41 +080077 if (!line)
78 continue;
Bin Meng363849b2015-04-24 18:10:03 +080079
Bin Mengda5d4632015-07-15 16:23:40 +080080 debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
81 line, bus, device, func, 'A' + pin - 1);
Bin Meng363849b2015-04-24 18:10:03 +080082
Bin Meng8df01812016-02-01 01:40:57 -080083 pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
Bin Mengda5d4632015-07-15 16:23:40 +080084 }
Bin Meng363849b2015-04-24 18:10:03 +080085}