Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2016 Google Inc |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 17 | #include <pci/pci.h> |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 18 | #include "pci-userspace.h" |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 19 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 20 | #define DEBUG_PCI 1 |
| 21 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 22 | static struct pci_access *pacc; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 23 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 24 | int pci_initialize(void) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 25 | { |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 26 | struct pci_dev *dev; |
| 27 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 28 | pacc = pci_alloc(); |
| 29 | |
| 30 | pci_init(pacc); |
| 31 | pci_scan_bus(pacc); |
| 32 | for (dev = pacc->devices; dev; dev = dev->next) { |
| 33 | pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES); |
| 34 | } |
| 35 | return 0; |
| 36 | } |
| 37 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 38 | int pci_exit(void) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 39 | { |
| 40 | pci_cleanup(pacc); |
| 41 | return 0; |
| 42 | } |
| 43 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 44 | u8 pci_read_config8(struct device *dev, unsigned int where) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 45 | { |
| 46 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 47 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 48 | return pci_read_byte(d, where); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 49 | #ifdef DEBUG_PCI |
| 50 | printf("PCI: device not found while read byte (%x:%x.%x)\n", |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 51 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 52 | #endif |
| 53 | return 0; |
| 54 | } |
| 55 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 56 | u16 pci_read_config16(struct device *dev, unsigned int where) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 57 | { |
| 58 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 59 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 60 | return pci_read_word(d, where); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 61 | #ifdef DEBUG_PCI |
| 62 | printf("PCI: device not found while read word (%x:%x.%x)\n", |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 63 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 64 | #endif |
| 65 | return 0; |
| 66 | } |
| 67 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 68 | u32 pci_read_config32(struct device *dev, unsigned int where) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 69 | { |
| 70 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 71 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 72 | return pci_read_long(d, where); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 73 | #ifdef DEBUG_PCI |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 74 | printf("PCI: device not found while read dword (%x:%x.%x)\n", |
| 75 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 76 | #endif |
| 77 | return 0; |
| 78 | } |
| 79 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 80 | void pci_write_config8(struct device *dev, unsigned int where, u8 val) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 81 | { |
| 82 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 83 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 84 | pci_write_byte(d, where, val); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 85 | #ifdef DEBUG_PCI |
| 86 | else |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 87 | printf("PCI: device not found while write byte (%x:%x.%x)\n", |
| 88 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 89 | #endif |
| 90 | } |
| 91 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 92 | void pci_write_config16(struct device *dev, unsigned int where, u16 val) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 93 | { |
| 94 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 95 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 96 | pci_write_word(d, where, val); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 97 | #ifdef DEBUG_PCI |
| 98 | else |
| 99 | printf("PCI: device not found while write word (%x:%x.%x)\n", |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 100 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 101 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 104 | void pci_write_config32(struct device *dev, unsigned int where, u32 val) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 105 | { |
| 106 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 107 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 108 | pci_write_long(d, where, val); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 109 | #ifdef DEBUG_PCI |
| 110 | else |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 111 | printf("PCI: device not found while write dword (%x:%x.%x)\n", |
| 112 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 113 | #endif |
| 114 | } |