blob: f8da08035e62bbcfd5454682bfa5b03fbeca19a9 [file] [log] [blame]
Simon Glassd188b182014-11-12 22:42:11 -07001/*
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>
Simon Glassa219dae2015-03-05 12:25:31 -070013#include <dm.h>
Simon Glass7430f102014-11-12 22:42:12 -070014#include <errno.h>
15#include <malloc.h>
Simon Glassd188b182014-11-12 22:42:11 -070016#include <pci.h>
Simon Glassa219dae2015-03-05 12:25:31 -070017#include <asm/io.h>
Simon Glassd188b182014-11-12 22:42:11 -070018#include <asm/pci.h>
19
Bin Meng4722c032014-12-30 22:53:19 +080020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassd188b182014-11-12 22:42:11 -070022static struct pci_controller x86_hose;
23
Simon Glass7430f102014-11-12 22:42:12 -070024int pci_early_init_hose(struct pci_controller **hosep)
25{
26 struct pci_controller *hose;
27
28 hose = calloc(1, sizeof(struct pci_controller));
29 if (!hose)
30 return -ENOMEM;
31
32 board_pci_setup_hose(hose);
33 pci_setup_type1(hose);
Bin Mengfa5530b2014-12-30 22:53:20 +080034 hose->last_busno = pci_hose_scan(hose);
Bin Meng8f9052f2014-12-30 22:53:21 +080035 gd->hose = hose;
Simon Glass7430f102014-11-12 22:42:12 -070036 *hosep = hose;
37
38 return 0;
39}
40
Simon Glasse94ea6f2014-11-14 18:18:28 -070041__weak int board_pci_pre_scan(struct pci_controller *hose)
42{
43 return 0;
44}
45
46__weak int board_pci_post_scan(struct pci_controller *hose)
47{
48 return 0;
49}
50
Simon Glassd188b182014-11-12 22:42:11 -070051void pci_init_board(void)
52{
53 struct pci_controller *hose = &x86_hose;
54
Simon Glass7430f102014-11-12 22:42:12 -070055 /* Stop using the early hose */
Bin Meng8f9052f2014-12-30 22:53:21 +080056 gd->hose = NULL;
Simon Glass7430f102014-11-12 22:42:12 -070057
Simon Glassd188b182014-11-12 22:42:11 -070058 board_pci_setup_hose(hose);
59 pci_setup_type1(hose);
60 pci_register_hose(hose);
61
Simon Glasse94ea6f2014-11-14 18:18:28 -070062 board_pci_pre_scan(hose);
Simon Glassd188b182014-11-12 22:42:11 -070063 hose->last_busno = pci_hose_scan(hose);
Simon Glasse94ea6f2014-11-14 18:18:28 -070064 board_pci_post_scan(hose);
Simon Glassd188b182014-11-12 22:42:11 -070065}
Simon Glass6fb3b722014-11-12 22:42:14 -070066
67static struct pci_controller *get_hose(void)
68{
Bin Meng8f9052f2014-12-30 22:53:21 +080069 if (gd->hose)
70 return gd->hose;
Simon Glass6fb3b722014-11-12 22:42:14 -070071
72 return pci_bus_to_hose(0);
73}
74
Simon Glass31f57c22015-03-05 12:25:15 -070075unsigned int x86_pci_read_config8(pci_dev_t dev, unsigned where)
Simon Glass6fb3b722014-11-12 22:42:14 -070076{
77 uint8_t value;
78
79 pci_hose_read_config_byte(get_hose(), dev, where, &value);
80
81 return value;
82}
83
Simon Glass31f57c22015-03-05 12:25:15 -070084unsigned int x86_pci_read_config16(pci_dev_t dev, unsigned where)
Simon Glass6fb3b722014-11-12 22:42:14 -070085{
86 uint16_t value;
87
88 pci_hose_read_config_word(get_hose(), dev, where, &value);
89
90 return value;
91}
92
Simon Glass31f57c22015-03-05 12:25:15 -070093unsigned int x86_pci_read_config32(pci_dev_t dev, unsigned where)
Simon Glass6fb3b722014-11-12 22:42:14 -070094{
95 uint32_t value;
96
97 pci_hose_read_config_dword(get_hose(), dev, where, &value);
98
99 return value;
100}
101
Simon Glass31f57c22015-03-05 12:25:15 -0700102void x86_pci_write_config8(pci_dev_t dev, unsigned where, unsigned value)
Simon Glass6fb3b722014-11-12 22:42:14 -0700103{
104 pci_hose_write_config_byte(get_hose(), dev, where, value);
105}
106
Simon Glass31f57c22015-03-05 12:25:15 -0700107void x86_pci_write_config16(pci_dev_t dev, unsigned where, unsigned value)
Simon Glass6fb3b722014-11-12 22:42:14 -0700108{
109 pci_hose_write_config_word(get_hose(), dev, where, value);
110}
111
Simon Glass31f57c22015-03-05 12:25:15 -0700112void x86_pci_write_config32(pci_dev_t dev, unsigned where, unsigned value)
Simon Glass6fb3b722014-11-12 22:42:14 -0700113{
114 pci_hose_write_config_dword(get_hose(), dev, where, value);
115}
Simon Glassa219dae2015-03-05 12:25:31 -0700116
117int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
118 ulong *valuep, enum pci_size_t size)
119{
120 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
121 switch (size) {
122 case PCI_SIZE_8:
123 *valuep = inb(PCI_REG_DATA + (offset & 3));
124 break;
125 case PCI_SIZE_16:
126 *valuep = inw(PCI_REG_DATA + (offset & 2));
127 break;
128 case PCI_SIZE_32:
129 *valuep = inl(PCI_REG_DATA);
130 break;
131 }
132
133 return 0;
134}
135
136int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
137 ulong value, enum pci_size_t size)
138{
139 outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
140 switch (size) {
141 case PCI_SIZE_8:
142 outb(value, PCI_REG_DATA + (offset & 3));
143 break;
144 case PCI_SIZE_16:
145 outw(value, PCI_REG_DATA + (offset & 2));
146 break;
147 case PCI_SIZE_32:
148 outl(value, PCI_REG_DATA);
149 break;
150 }
151
152 return 0;
153}
Bin Menge3e7fa22015-04-24 18:10:03 +0800154
Bin Meng31a2dc62015-07-15 16:23:40 +0800155void pci_assign_irqs(int bus, int device, u8 irq[4])
Bin Menge3e7fa22015-04-24 18:10:03 +0800156{
157 pci_dev_t bdf;
Bin Meng31a2dc62015-07-15 16:23:40 +0800158 int func;
159 u16 vendor;
Bin Menge3e7fa22015-04-24 18:10:03 +0800160 u8 pin, line;
161
Bin Meng31a2dc62015-07-15 16:23:40 +0800162 for (func = 0; func < 8; func++) {
163 bdf = PCI_BDF(bus, device, func);
164 vendor = x86_pci_read_config16(bdf, PCI_VENDOR_ID);
165 if (vendor == 0xffff || vendor == 0x0000)
166 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +0800167
Bin Meng31a2dc62015-07-15 16:23:40 +0800168 pin = x86_pci_read_config8(bdf, PCI_INTERRUPT_PIN);
Bin Menge3e7fa22015-04-24 18:10:03 +0800169
Bin Meng31a2dc62015-07-15 16:23:40 +0800170 /* PCI spec says all values except 1..4 are reserved */
171 if ((pin < 1) || (pin > 4))
172 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +0800173
Bin Meng31a2dc62015-07-15 16:23:40 +0800174 line = irq[pin - 1];
Bin Meng6fc0e8a2015-07-15 16:23:41 +0800175 if (!line)
176 continue;
Bin Menge3e7fa22015-04-24 18:10:03 +0800177
Bin Meng31a2dc62015-07-15 16:23:40 +0800178 debug("Assigning IRQ %d to PCI device %d.%x.%d (INT%c)\n",
179 line, bus, device, func, 'A' + pin - 1);
Bin Menge3e7fa22015-04-24 18:10:03 +0800180
Bin Meng31a2dc62015-07-15 16:23:40 +0800181 x86_pci_write_config8(bdf, PCI_INTERRUPT_LINE, line);
182 }
Bin Menge3e7fa22015-04-24 18:10:03 +0800183}