Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 2 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 4 | #include <pci/pci.h> |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 5 | #include "pci-userspace.h" |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 6 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 7 | #define DEBUG_PCI 1 |
| 8 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 9 | static struct pci_access *pacc; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 10 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 11 | int pci_initialize(void) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 12 | { |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 13 | struct pci_dev *dev; |
| 14 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 15 | pacc = pci_alloc(); |
| 16 | |
| 17 | pci_init(pacc); |
| 18 | pci_scan_bus(pacc); |
| 19 | for (dev = pacc->devices; dev; dev = dev->next) { |
| 20 | pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES); |
| 21 | } |
| 22 | return 0; |
| 23 | } |
| 24 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 25 | int pci_exit(void) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 26 | { |
| 27 | pci_cleanup(pacc); |
| 28 | return 0; |
| 29 | } |
| 30 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 31 | u8 pci_read_config8(struct device *dev, unsigned int where) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 32 | { |
| 33 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 34 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 35 | return pci_read_byte(d, where); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 36 | #ifdef DEBUG_PCI |
| 37 | printf("PCI: device not found while read byte (%x:%x.%x)\n", |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 38 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 39 | #endif |
| 40 | return 0; |
| 41 | } |
| 42 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 43 | u16 pci_read_config16(struct device *dev, unsigned int where) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 44 | { |
| 45 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 46 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 47 | return pci_read_word(d, where); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 48 | #ifdef DEBUG_PCI |
| 49 | printf("PCI: device not found while read word (%x:%x.%x)\n", |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 50 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 51 | #endif |
| 52 | return 0; |
| 53 | } |
| 54 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 55 | u32 pci_read_config32(struct device *dev, unsigned int where) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 56 | { |
| 57 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 58 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 59 | return pci_read_long(d, where); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 60 | #ifdef DEBUG_PCI |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 61 | printf("PCI: device not found while read dword (%x:%x.%x)\n", |
| 62 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 63 | #endif |
| 64 | return 0; |
| 65 | } |
| 66 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 67 | 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] | 68 | { |
| 69 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 70 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 71 | pci_write_byte(d, where, val); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 72 | #ifdef DEBUG_PCI |
| 73 | else |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 74 | printf("PCI: device not found while write byte (%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 | } |
| 78 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 82 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 83 | pci_write_word(d, where, val); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 84 | #ifdef DEBUG_PCI |
| 85 | else |
| 86 | printf("PCI: device not found while write word (%x:%x.%x)\n", |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 87 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 88 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 91 | 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] | 92 | { |
| 93 | struct pci_dev *d; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 94 | if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func))) |
| 95 | pci_write_long(d, where, val); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 96 | #ifdef DEBUG_PCI |
| 97 | else |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 98 | printf("PCI: device not found while write dword (%x:%x.%x)\n", |
| 99 | dev->busno, dev->slot, dev->func); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 100 | #endif |
| 101 | } |