Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 7680550 | 2014-11-12 22:42:11 -0700 | [diff] [blame] | 2 | /* |
| 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 Glass | 7680550 | 2014-11-12 22:42:11 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 4e03781 | 2015-03-05 12:25:31 -0700 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | a54d981 | 2014-11-12 22:42:12 -0700 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | #include <malloc.h> |
Simon Glass | 7680550 | 2014-11-12 22:42:11 -0700 | [diff] [blame] | 15 | #include <pci.h> |
Simon Glass | 4e03781 | 2015-03-05 12:25:31 -0700 | [diff] [blame] | 16 | #include <asm/io.h> |
Simon Glass | 7680550 | 2014-11-12 22:42:11 -0700 | [diff] [blame] | 17 | #include <asm/pci.h> |
| 18 | |
Simon Glass | a546458 | 2019-08-31 21:23:18 -0600 | [diff] [blame] | 19 | int pci_x86_read_config(pci_dev_t bdf, uint offset, ulong *valuep, |
| 20 | enum pci_size_t size) |
Simon Glass | 4e03781 | 2015-03-05 12:25:31 -0700 | [diff] [blame] | 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 | |
Simon Glass | a546458 | 2019-08-31 21:23:18 -0600 | [diff] [blame] | 38 | int pci_x86_write_config(pci_dev_t bdf, uint offset, ulong value, |
| 39 | enum pci_size_t size) |
Simon Glass | 4e03781 | 2015-03-05 12:25:31 -0700 | [diff] [blame] | 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 Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 56 | |
Simon Glass | a546458 | 2019-08-31 21:23:18 -0600 | [diff] [blame] | 57 | int pci_x86_clrset_config(pci_dev_t bdf, uint offset, ulong clr, ulong set, |
| 58 | enum pci_size_t size) |
Simon Glass | 7567787 | 2019-09-25 08:11:37 -0600 | [diff] [blame] | 59 | { |
| 60 | ulong value; |
| 61 | int ret; |
| 62 | |
Simon Glass | a546458 | 2019-08-31 21:23:18 -0600 | [diff] [blame] | 63 | ret = pci_x86_read_config(bdf, offset, &value, size); |
Simon Glass | 7567787 | 2019-09-25 08:11:37 -0600 | [diff] [blame] | 64 | if (ret) |
| 65 | return ret; |
| 66 | value &= ~clr; |
| 67 | value |= set; |
| 68 | |
Simon Glass | a546458 | 2019-08-31 21:23:18 -0600 | [diff] [blame] | 69 | return pci_x86_write_config(bdf, offset, value, size); |
Simon Glass | 7567787 | 2019-09-25 08:11:37 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 72 | void pci_assign_irqs(int bus, int device, u8 irq[4]) |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 73 | { |
| 74 | pci_dev_t bdf; |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 75 | int func; |
| 76 | u16 vendor; |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 77 | u8 pin, line; |
| 78 | |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 79 | for (func = 0; func < 8; func++) { |
| 80 | bdf = PCI_BDF(bus, device, func); |
Bin Meng | 8df0181 | 2016-02-01 01:40:57 -0800 | [diff] [blame] | 81 | pci_read_config16(bdf, PCI_VENDOR_ID, &vendor); |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 82 | if (vendor == 0xffff || vendor == 0x0000) |
| 83 | continue; |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 84 | |
Bin Meng | 8df0181 | 2016-02-01 01:40:57 -0800 | [diff] [blame] | 85 | pci_read_config8(bdf, PCI_INTERRUPT_PIN, &pin); |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 86 | |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 87 | /* PCI spec says all values except 1..4 are reserved */ |
| 88 | if ((pin < 1) || (pin > 4)) |
| 89 | continue; |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 90 | |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 91 | line = irq[pin - 1]; |
Bin Meng | e0a5fd9 | 2015-07-15 16:23:41 +0800 | [diff] [blame] | 92 | if (!line) |
| 93 | continue; |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 94 | |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 95 | debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n", |
| 96 | line, bus, device, func, 'A' + pin - 1); |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 97 | |
Bin Meng | 8df0181 | 2016-02-01 01:40:57 -0800 | [diff] [blame] | 98 | pci_write_config8(bdf, PCI_INTERRUPT_LINE, line); |
Bin Meng | da5d463 | 2015-07-15 16:23:40 +0800 | [diff] [blame] | 99 | } |
Bin Meng | 363849b | 2015-04-24 18:10:03 +0800 | [diff] [blame] | 100 | } |