404fbb68609d9fc2c55dbffe3de86fcdc1ff52f4
[oweals/u-boot.git] / arch / x86 / cpu / pci.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2008,2009
4  * Graeme Russ, <graeme.russ@gmail.com>
5  *
6  * (C) Copyright 2002
7  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <common.h>
13 #include <errno.h>
14 #include <malloc.h>
15 #include <pci.h>
16 #include <asm/pci.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 static struct pci_controller x86_hose;
21
22 int pci_early_init_hose(struct pci_controller **hosep)
23 {
24         struct pci_controller *hose;
25
26         hose = calloc(1, sizeof(struct pci_controller));
27         if (!hose)
28                 return -ENOMEM;
29
30         board_pci_setup_hose(hose);
31         pci_setup_type1(hose);
32         gd->arch.hose = hose;
33         *hosep = hose;
34
35         return 0;
36 }
37
38 __weak int board_pci_pre_scan(struct pci_controller *hose)
39 {
40         return 0;
41 }
42
43 __weak int board_pci_post_scan(struct pci_controller *hose)
44 {
45         return 0;
46 }
47
48 void pci_init_board(void)
49 {
50         struct pci_controller *hose = &x86_hose;
51
52         /* Stop using the early hose */
53         gd->arch.hose = NULL;
54
55         board_pci_setup_hose(hose);
56         pci_setup_type1(hose);
57         pci_register_hose(hose);
58
59         board_pci_pre_scan(hose);
60         hose->last_busno = pci_hose_scan(hose);
61         board_pci_post_scan(hose);
62 }
63
64 static struct pci_controller *get_hose(void)
65 {
66         if (gd->arch.hose)
67                 return gd->arch.hose;
68
69         return pci_bus_to_hose(0);
70 }
71
72 unsigned int pci_read_config8(pci_dev_t dev, unsigned where)
73 {
74         uint8_t value;
75
76         pci_hose_read_config_byte(get_hose(), dev, where, &value);
77
78         return value;
79 }
80
81 unsigned int pci_read_config16(pci_dev_t dev, unsigned where)
82 {
83         uint16_t value;
84
85         pci_hose_read_config_word(get_hose(), dev, where, &value);
86
87         return value;
88 }
89
90 unsigned int pci_read_config32(pci_dev_t dev, unsigned where)
91 {
92         uint32_t value;
93
94         pci_hose_read_config_dword(get_hose(), dev, where, &value);
95
96         return value;
97 }
98
99 void pci_write_config8(pci_dev_t dev, unsigned where, unsigned value)
100 {
101         pci_hose_write_config_byte(get_hose(), dev, where, value);
102 }
103
104 void pci_write_config16(pci_dev_t dev, unsigned where, unsigned value)
105 {
106         pci_hose_write_config_word(get_hose(), dev, where, value);
107 }
108
109 void pci_write_config32(pci_dev_t dev, unsigned where, unsigned value)
110 {
111         pci_hose_write_config_dword(get_hose(), dev, where, value);
112 }