xref: /openbmc/u-boot/arch/x86/cpu/pci.c (revision a219daea)
1d188b18fSSimon Glass /*
2d188b18fSSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
3d188b18fSSimon Glass  * (C) Copyright 2008,2009
4d188b18fSSimon Glass  * Graeme Russ, <graeme.russ@gmail.com>
5d188b18fSSimon Glass  *
6d188b18fSSimon Glass  * (C) Copyright 2002
7d188b18fSSimon Glass  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
8d188b18fSSimon Glass  *
9d188b18fSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
10d188b18fSSimon Glass  */
11d188b18fSSimon Glass 
12d188b18fSSimon Glass #include <common.h>
13*a219daeaSSimon Glass #include <dm.h>
147430f108SSimon Glass #include <errno.h>
157430f108SSimon Glass #include <malloc.h>
16d188b18fSSimon Glass #include <pci.h>
17*a219daeaSSimon Glass #include <asm/io.h>
18d188b18fSSimon Glass #include <asm/pci.h>
19d188b18fSSimon Glass 
204722c035SBin Meng DECLARE_GLOBAL_DATA_PTR;
214722c035SBin Meng 
22d188b18fSSimon Glass static struct pci_controller x86_hose;
23d188b18fSSimon Glass 
247430f108SSimon Glass int pci_early_init_hose(struct pci_controller **hosep)
257430f108SSimon Glass {
267430f108SSimon Glass 	struct pci_controller *hose;
277430f108SSimon Glass 
287430f108SSimon Glass 	hose = calloc(1, sizeof(struct pci_controller));
297430f108SSimon Glass 	if (!hose)
307430f108SSimon Glass 		return -ENOMEM;
317430f108SSimon Glass 
327430f108SSimon Glass 	board_pci_setup_hose(hose);
337430f108SSimon Glass 	pci_setup_type1(hose);
34fa5530b8SBin Meng 	hose->last_busno = pci_hose_scan(hose);
358f9052fdSBin Meng 	gd->hose = hose;
367430f108SSimon Glass 	*hosep = hose;
377430f108SSimon Glass 
387430f108SSimon Glass 	return 0;
397430f108SSimon Glass }
407430f108SSimon Glass 
41e94ea6f6SSimon Glass __weak int board_pci_pre_scan(struct pci_controller *hose)
42e94ea6f6SSimon Glass {
43e94ea6f6SSimon Glass 	return 0;
44e94ea6f6SSimon Glass }
45e94ea6f6SSimon Glass 
46e94ea6f6SSimon Glass __weak int board_pci_post_scan(struct pci_controller *hose)
47e94ea6f6SSimon Glass {
48e94ea6f6SSimon Glass 	return 0;
49e94ea6f6SSimon Glass }
50e94ea6f6SSimon Glass 
51d188b18fSSimon Glass void pci_init_board(void)
52d188b18fSSimon Glass {
53d188b18fSSimon Glass 	struct pci_controller *hose = &x86_hose;
54d188b18fSSimon Glass 
557430f108SSimon Glass 	/* Stop using the early hose */
568f9052fdSBin Meng 	gd->hose = NULL;
577430f108SSimon Glass 
58d188b18fSSimon Glass 	board_pci_setup_hose(hose);
59d188b18fSSimon Glass 	pci_setup_type1(hose);
60d188b18fSSimon Glass 	pci_register_hose(hose);
61d188b18fSSimon Glass 
62e94ea6f6SSimon Glass 	board_pci_pre_scan(hose);
63d188b18fSSimon Glass 	hose->last_busno = pci_hose_scan(hose);
64e94ea6f6SSimon Glass 	board_pci_post_scan(hose);
65d188b18fSSimon Glass }
666fb3b72eSSimon Glass 
676fb3b72eSSimon Glass static struct pci_controller *get_hose(void)
686fb3b72eSSimon Glass {
698f9052fdSBin Meng 	if (gd->hose)
708f9052fdSBin Meng 		return gd->hose;
716fb3b72eSSimon Glass 
726fb3b72eSSimon Glass 	return pci_bus_to_hose(0);
736fb3b72eSSimon Glass }
746fb3b72eSSimon Glass 
7531f57c28SSimon Glass unsigned int x86_pci_read_config8(pci_dev_t dev, unsigned where)
766fb3b72eSSimon Glass {
776fb3b72eSSimon Glass 	uint8_t value;
786fb3b72eSSimon Glass 
796fb3b72eSSimon Glass 	pci_hose_read_config_byte(get_hose(), dev, where, &value);
806fb3b72eSSimon Glass 
816fb3b72eSSimon Glass 	return value;
826fb3b72eSSimon Glass }
836fb3b72eSSimon Glass 
8431f57c28SSimon Glass unsigned int x86_pci_read_config16(pci_dev_t dev, unsigned where)
856fb3b72eSSimon Glass {
866fb3b72eSSimon Glass 	uint16_t value;
876fb3b72eSSimon Glass 
886fb3b72eSSimon Glass 	pci_hose_read_config_word(get_hose(), dev, where, &value);
896fb3b72eSSimon Glass 
906fb3b72eSSimon Glass 	return value;
916fb3b72eSSimon Glass }
926fb3b72eSSimon Glass 
9331f57c28SSimon Glass unsigned int x86_pci_read_config32(pci_dev_t dev, unsigned where)
946fb3b72eSSimon Glass {
956fb3b72eSSimon Glass 	uint32_t value;
966fb3b72eSSimon Glass 
976fb3b72eSSimon Glass 	pci_hose_read_config_dword(get_hose(), dev, where, &value);
986fb3b72eSSimon Glass 
996fb3b72eSSimon Glass 	return value;
1006fb3b72eSSimon Glass }
1016fb3b72eSSimon Glass 
10231f57c28SSimon Glass void x86_pci_write_config8(pci_dev_t dev, unsigned where, unsigned value)
1036fb3b72eSSimon Glass {
1046fb3b72eSSimon Glass 	pci_hose_write_config_byte(get_hose(), dev, where, value);
1056fb3b72eSSimon Glass }
1066fb3b72eSSimon Glass 
10731f57c28SSimon Glass void x86_pci_write_config16(pci_dev_t dev, unsigned where, unsigned value)
1086fb3b72eSSimon Glass {
1096fb3b72eSSimon Glass 	pci_hose_write_config_word(get_hose(), dev, where, value);
1106fb3b72eSSimon Glass }
1116fb3b72eSSimon Glass 
11231f57c28SSimon Glass void x86_pci_write_config32(pci_dev_t dev, unsigned where, unsigned value)
1136fb3b72eSSimon Glass {
1146fb3b72eSSimon Glass 	pci_hose_write_config_dword(get_hose(), dev, where, value);
1156fb3b72eSSimon Glass }
116*a219daeaSSimon Glass 
117*a219daeaSSimon Glass int pci_x86_read_config(struct udevice *bus, pci_dev_t bdf, uint offset,
118*a219daeaSSimon Glass 			ulong *valuep, enum pci_size_t size)
119*a219daeaSSimon Glass {
120*a219daeaSSimon Glass 	outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
121*a219daeaSSimon Glass 	switch (size) {
122*a219daeaSSimon Glass 	case PCI_SIZE_8:
123*a219daeaSSimon Glass 		*valuep = inb(PCI_REG_DATA + (offset & 3));
124*a219daeaSSimon Glass 		break;
125*a219daeaSSimon Glass 	case PCI_SIZE_16:
126*a219daeaSSimon Glass 		*valuep = inw(PCI_REG_DATA + (offset & 2));
127*a219daeaSSimon Glass 		break;
128*a219daeaSSimon Glass 	case PCI_SIZE_32:
129*a219daeaSSimon Glass 		*valuep = inl(PCI_REG_DATA);
130*a219daeaSSimon Glass 		break;
131*a219daeaSSimon Glass 	}
132*a219daeaSSimon Glass 
133*a219daeaSSimon Glass 	return 0;
134*a219daeaSSimon Glass }
135*a219daeaSSimon Glass 
136*a219daeaSSimon Glass int pci_x86_write_config(struct udevice *bus, pci_dev_t bdf, uint offset,
137*a219daeaSSimon Glass 			 ulong value, enum pci_size_t size)
138*a219daeaSSimon Glass {
139*a219daeaSSimon Glass 	outl(bdf | (offset & 0xfc) | PCI_CFG_EN, PCI_REG_ADDR);
140*a219daeaSSimon Glass 	switch (size) {
141*a219daeaSSimon Glass 	case PCI_SIZE_8:
142*a219daeaSSimon Glass 		outb(value, PCI_REG_DATA + (offset & 3));
143*a219daeaSSimon Glass 		break;
144*a219daeaSSimon Glass 	case PCI_SIZE_16:
145*a219daeaSSimon Glass 		outw(value, PCI_REG_DATA + (offset & 2));
146*a219daeaSSimon Glass 		break;
147*a219daeaSSimon Glass 	case PCI_SIZE_32:
148*a219daeaSSimon Glass 		outl(value, PCI_REG_DATA);
149*a219daeaSSimon Glass 		break;
150*a219daeaSSimon Glass 	}
151*a219daeaSSimon Glass 
152*a219daeaSSimon Glass 	return 0;
153*a219daeaSSimon Glass }
154