blob: 5d8a7fedf0b2aacc046d7642178a053e1be0589c [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer05082732015-10-21 13:00:41 -07002
Li-Ta Lo81521262004-07-08 17:18:27 +00003#include <stdio.h>
Stefan Reinauer05082732015-10-21 13:00:41 -07004#include <pci/pci.h>
Stefan Reinauer850e7d42015-09-28 13:12:04 -07005#include "pci-userspace.h"
Li-Ta Lo81521262004-07-08 17:18:27 +00006
Li-Ta Lo81521262004-07-08 17:18:27 +00007#define DEBUG_PCI 1
8
Stefan Reinauer05082732015-10-21 13:00:41 -07009static struct pci_access *pacc;
Li-Ta Lo81521262004-07-08 17:18:27 +000010
Stefan Reinauer05082732015-10-21 13:00:41 -070011int pci_initialize(void)
Li-Ta Lo81521262004-07-08 17:18:27 +000012{
Stefan Reinauer05082732015-10-21 13:00:41 -070013 struct pci_dev *dev;
14
Li-Ta Lo81521262004-07-08 17:18:27 +000015 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 Reinauer05082732015-10-21 13:00:41 -070025int pci_exit(void)
Li-Ta Lo81521262004-07-08 17:18:27 +000026{
27 pci_cleanup(pacc);
28 return 0;
29}
30
Stefan Reinauer05082732015-10-21 13:00:41 -070031u8 pci_read_config8(struct device *dev, unsigned int where)
Li-Ta Lo81521262004-07-08 17:18:27 +000032{
33 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070034 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
35 return pci_read_byte(d, where);
Li-Ta Lo81521262004-07-08 17:18:27 +000036#ifdef DEBUG_PCI
37 printf("PCI: device not found while read byte (%x:%x.%x)\n",
Stefan Reinauer05082732015-10-21 13:00:41 -070038 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000039#endif
40 return 0;
41}
42
Stefan Reinauer05082732015-10-21 13:00:41 -070043u16 pci_read_config16(struct device *dev, unsigned int where)
Li-Ta Lo81521262004-07-08 17:18:27 +000044{
45 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070046 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
47 return pci_read_word(d, where);
Li-Ta Lo81521262004-07-08 17:18:27 +000048#ifdef DEBUG_PCI
49 printf("PCI: device not found while read word (%x:%x.%x)\n",
Stefan Reinauer05082732015-10-21 13:00:41 -070050 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000051#endif
52 return 0;
53}
54
Stefan Reinauer05082732015-10-21 13:00:41 -070055u32 pci_read_config32(struct device *dev, unsigned int where)
Li-Ta Lo81521262004-07-08 17:18:27 +000056{
57 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070058 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
59 return pci_read_long(d, where);
Li-Ta Lo81521262004-07-08 17:18:27 +000060#ifdef DEBUG_PCI
Stefan Reinauer05082732015-10-21 13:00:41 -070061 printf("PCI: device not found while read dword (%x:%x.%x)\n",
62 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000063#endif
64 return 0;
65}
66
Stefan Reinauer05082732015-10-21 13:00:41 -070067void pci_write_config8(struct device *dev, unsigned int where, u8 val)
Li-Ta Lo81521262004-07-08 17:18:27 +000068{
69 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070070 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
71 pci_write_byte(d, where, val);
Li-Ta Lo81521262004-07-08 17:18:27 +000072#ifdef DEBUG_PCI
73 else
Stefan Reinauer05082732015-10-21 13:00:41 -070074 printf("PCI: device not found while write byte (%x:%x.%x)\n",
75 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000076#endif
77}
78
Stefan Reinauer05082732015-10-21 13:00:41 -070079void pci_write_config16(struct device *dev, unsigned int where, u16 val)
Li-Ta Lo81521262004-07-08 17:18:27 +000080{
81 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070082 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
83 pci_write_word(d, where, val);
Li-Ta Lo81521262004-07-08 17:18:27 +000084#ifdef DEBUG_PCI
85 else
86 printf("PCI: device not found while write word (%x:%x.%x)\n",
Stefan Reinauer05082732015-10-21 13:00:41 -070087 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000088#endif
Li-Ta Lo81521262004-07-08 17:18:27 +000089}
90
Stefan Reinauer05082732015-10-21 13:00:41 -070091void pci_write_config32(struct device *dev, unsigned int where, u32 val)
Li-Ta Lo81521262004-07-08 17:18:27 +000092{
93 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070094 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
95 pci_write_long(d, where, val);
Li-Ta Lo81521262004-07-08 17:18:27 +000096#ifdef DEBUG_PCI
97 else
Stefan Reinauer05082732015-10-21 13:00:41 -070098 printf("PCI: device not found while write dword (%x:%x.%x)\n",
99 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +0000100#endif
101}