blob: 0390f7a494cb013d147fcaf6c9ad0bd2c158f045 [file] [log] [blame]
Stefan Reinauer05082732015-10-21 13:00:41 -07001/*
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 Lo81521262004-07-08 17:18:27 +000016#include <stdio.h>
Stefan Reinauer05082732015-10-21 13:00:41 -070017#include <pci/pci.h>
Stefan Reinauer850e7d42015-09-28 13:12:04 -070018#include "pci-userspace.h"
Li-Ta Lo81521262004-07-08 17:18:27 +000019
Li-Ta Lo81521262004-07-08 17:18:27 +000020#define DEBUG_PCI 1
21
Stefan Reinauer05082732015-10-21 13:00:41 -070022static struct pci_access *pacc;
Li-Ta Lo81521262004-07-08 17:18:27 +000023
Stefan Reinauer05082732015-10-21 13:00:41 -070024int pci_initialize(void)
Li-Ta Lo81521262004-07-08 17:18:27 +000025{
Stefan Reinauer05082732015-10-21 13:00:41 -070026 struct pci_dev *dev;
27
Li-Ta Lo81521262004-07-08 17:18:27 +000028 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 Reinauer05082732015-10-21 13:00:41 -070038int pci_exit(void)
Li-Ta Lo81521262004-07-08 17:18:27 +000039{
40 pci_cleanup(pacc);
41 return 0;
42}
43
Stefan Reinauer05082732015-10-21 13:00:41 -070044u8 pci_read_config8(struct device *dev, unsigned int where)
Li-Ta Lo81521262004-07-08 17:18:27 +000045{
46 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070047 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
48 return pci_read_byte(d, where);
Li-Ta Lo81521262004-07-08 17:18:27 +000049#ifdef DEBUG_PCI
50 printf("PCI: device not found while read byte (%x:%x.%x)\n",
Stefan Reinauer05082732015-10-21 13:00:41 -070051 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000052#endif
53 return 0;
54}
55
Stefan Reinauer05082732015-10-21 13:00:41 -070056u16 pci_read_config16(struct device *dev, unsigned int where)
Li-Ta Lo81521262004-07-08 17:18:27 +000057{
58 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070059 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
60 return pci_read_word(d, where);
Li-Ta Lo81521262004-07-08 17:18:27 +000061#ifdef DEBUG_PCI
62 printf("PCI: device not found while read word (%x:%x.%x)\n",
Stefan Reinauer05082732015-10-21 13:00:41 -070063 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000064#endif
65 return 0;
66}
67
Stefan Reinauer05082732015-10-21 13:00:41 -070068u32 pci_read_config32(struct device *dev, unsigned int where)
Li-Ta Lo81521262004-07-08 17:18:27 +000069{
70 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070071 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
72 return pci_read_long(d, where);
Li-Ta Lo81521262004-07-08 17:18:27 +000073#ifdef DEBUG_PCI
Stefan Reinauer05082732015-10-21 13:00:41 -070074 printf("PCI: device not found while read dword (%x:%x.%x)\n",
75 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000076#endif
77 return 0;
78}
79
Stefan Reinauer05082732015-10-21 13:00:41 -070080void pci_write_config8(struct device *dev, unsigned int where, u8 val)
Li-Ta Lo81521262004-07-08 17:18:27 +000081{
82 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070083 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
84 pci_write_byte(d, where, val);
Li-Ta Lo81521262004-07-08 17:18:27 +000085#ifdef DEBUG_PCI
86 else
Stefan Reinauer05082732015-10-21 13:00:41 -070087 printf("PCI: device not found while write byte (%x:%x.%x)\n",
88 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +000089#endif
90}
91
Stefan Reinauer05082732015-10-21 13:00:41 -070092void pci_write_config16(struct device *dev, unsigned int where, u16 val)
Li-Ta Lo81521262004-07-08 17:18:27 +000093{
94 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -070095 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
96 pci_write_word(d, where, val);
Li-Ta Lo81521262004-07-08 17:18:27 +000097#ifdef DEBUG_PCI
98 else
99 printf("PCI: device not found while write word (%x:%x.%x)\n",
Stefan Reinauer05082732015-10-21 13:00:41 -0700100 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +0000101#endif
Li-Ta Lo81521262004-07-08 17:18:27 +0000102}
103
Stefan Reinauer05082732015-10-21 13:00:41 -0700104void pci_write_config32(struct device *dev, unsigned int where, u32 val)
Li-Ta Lo81521262004-07-08 17:18:27 +0000105{
106 struct pci_dev *d;
Stefan Reinauer05082732015-10-21 13:00:41 -0700107 if ((d = pci_get_dev(pacc, 0, dev->busno, dev->slot, dev->func)))
108 pci_write_long(d, where, val);
Li-Ta Lo81521262004-07-08 17:18:27 +0000109#ifdef DEBUG_PCI
110 else
Stefan Reinauer05082732015-10-21 13:00:41 -0700111 printf("PCI: device not found while write dword (%x:%x.%x)\n",
112 dev->busno, dev->slot, dev->func);
Li-Ta Lo81521262004-07-08 17:18:27 +0000113#endif
114}