blob: 2728c00227d295abaa9e4cfb770d13ee8b07a15e [file] [log] [blame]
Simon Glassd188b182014-11-12 22:42:11 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * (C) Copyright 2008,2009
4 * Graeme Russ, <graeme.russ@gmail.com>
5 *
6 * (C) Copyright 2002
7 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 */
11
12#include <common.h>
Simon Glassa219dae2015-03-05 12:25:31 -070013#include <dm.h>
Simon Glass7430f102014-11-12 22:42:12 -070014#include <errno.h>
15#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 Glassa219dae2015-03-05 12:25:31 -070020int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
21 ulong *valuep, enum pci_size_t size)
22{
23 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
24 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
39int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
40 ulong value, enum pci_size_t size)
41{
42 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
43 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
Bin Meng31a2dc62015-07-15 16:23:40 +080058void pci_assign_irqs(int bus, int device, u8 irq[4])
Bin Menge3e7fa22015-04-24 18:10:03 +080059{
60 pci_dev_t bdf;
Bin Meng31a2dc62015-07-15 16:23:40 +080061 int func;
62 u16 vendor;
Bin Menge3e7fa22015-04-24 18:10:03 +080063 u8 pin, line;
64
Bin Meng31a2dc62015-07-15 16:23:40 +080065 for (func = 0; func < 8; func++) {
66 bdf = PCI_BDF(bus, device, func);
Bin Meng58316f92016-02-01 01:40:57 -080067 pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
Bin Meng31a2dc62015-07-15 16:23:40 +080068 if (vendor == 0xffff || vendor == 0x0000)
69 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080070
Bin Meng58316f92016-02-01 01:40:57 -080071 pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
Bin Menge3e7fa22015-04-24 18:10:03 +080072
Bin Meng31a2dc62015-07-15 16:23:40 +080073 /* PCI spec says all values except 1..4 are reserved */
74 if ((pin < 1) || (pin > 4))
75 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080076
Bin Meng31a2dc62015-07-15 16:23:40 +080077 line = irq[pin - 1];
Bin Meng6fc0e8a2015-07-15 16:23:41 +080078 if (!line)
79 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080080
Bin Meng31a2dc62015-07-15 16:23:40 +080081 debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
82 line, bus, device, func, 'A' + pin - 1);
Bin Menge3e7fa22015-04-24 18:10:03 +080083
Bin Meng58316f92016-02-01 01:40:57 -080084 pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
Bin Meng31a2dc62015-07-15 16:23:40 +080085 }
Bin Menge3e7fa22015-04-24 18:10:03 +080086}