blob: c9c7637fa7d761f8a8e2da8d686510e9a8d81c6c [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
Bin Meng4722c032014-12-30 22:53:19 +080020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassa219dae2015-03-05 12:25:31 -070022int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
23 ulong *valuep, enum pci_size_t size)
24{
25 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
26 switch (size) {
27 case PCI_SIZE_8:
28 *valuep = inb(PCI_REG_DATA + (offset & 3));
29 break;
30 case PCI_SIZE_16:
31 *valuep = inw(PCI_REG_DATA + (offset & 2));
32 break;
33 case PCI_SIZE_32:
34 *valuep = inl(PCI_REG_DATA);
35 break;
36 }
37
38 return 0;
39}
40
41int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
42 ulong value, enum pci_size_t size)
43{
44 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
45 switch (size) {
46 case PCI_SIZE_8:
47 outb(value, PCI_REG_DATA + (offset & 3));
48 break;
49 case PCI_SIZE_16:
50 outw(value, PCI_REG_DATA + (offset & 2));
51 break;
52 case PCI_SIZE_32:
53 outl(value, PCI_REG_DATA);
54 break;
55 }
56
57 return 0;
58}
Bin Menge3e7fa22015-04-24 18:10:03 +080059
Bin Meng31a2dc62015-07-15 16:23:40 +080060void pci_assign_irqs(int bus, int device, u8 irq[4])
Bin Menge3e7fa22015-04-24 18:10:03 +080061{
62 pci_dev_t bdf;
Bin Meng31a2dc62015-07-15 16:23:40 +080063 int func;
64 u16 vendor;
Bin Menge3e7fa22015-04-24 18:10:03 +080065 u8 pin, line;
66
Bin Meng31a2dc62015-07-15 16:23:40 +080067 for (func = 0; func < 8; func++) {
68 bdf = PCI_BDF(bus, device, func);
Bin Meng58316f92016-02-01 01:40:57 -080069 pci_read_config16(bdf, PCI_VENDOR_ID, &vendor);
Bin Meng31a2dc62015-07-15 16:23:40 +080070 if (vendor == 0xffff || vendor == 0x0000)
71 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080072
Bin Meng58316f92016-02-01 01:40:57 -080073 pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin);
Bin Menge3e7fa22015-04-24 18:10:03 +080074
Bin Meng31a2dc62015-07-15 16:23:40 +080075 /* PCI spec says all values except 1..4 are reserved */
76 if ((pin < 1) || (pin > 4))
77 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080078
Bin Meng31a2dc62015-07-15 16:23:40 +080079 line = irq[pin - 1];
Bin Meng6fc0e8a2015-07-15 16:23:41 +080080 if (!line)
81 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +080082
Bin Meng31a2dc62015-07-15 16:23:40 +080083 debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
84 line, bus, device, func, 'A' + pin - 1);
Bin Menge3e7fa22015-04-24 18:10:03 +080085
Bin Meng58316f92016-02-01 01:40:57 -080086 pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
Bin Meng31a2dc62015-07-15 16:23:40 +080087 }
Bin Menge3e7fa22015-04-24 18:10:03 +080088}