1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <pci.h>
9*4882a593Smuzhiyun #include <qfw.h>
10*4882a593Smuzhiyun #include <asm/irq.h>
11*4882a593Smuzhiyun #include <asm/post.h>
12*4882a593Smuzhiyun #include <asm/processor.h>
13*4882a593Smuzhiyun #include <asm/arch/device.h>
14*4882a593Smuzhiyun #include <asm/arch/qemu.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun static bool i440fx;
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #ifdef CONFIG_QFW
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /* on x86, the qfw registers are all IO ports */
21*4882a593Smuzhiyun #define FW_CONTROL_PORT 0x510
22*4882a593Smuzhiyun #define FW_DATA_PORT 0x511
23*4882a593Smuzhiyun #define FW_DMA_PORT_LOW 0x514
24*4882a593Smuzhiyun #define FW_DMA_PORT_HIGH 0x518
25*4882a593Smuzhiyun
qemu_x86_fwcfg_read_entry_pio(uint16_t entry,uint32_t size,void * address)26*4882a593Smuzhiyun static void qemu_x86_fwcfg_read_entry_pio(uint16_t entry,
27*4882a593Smuzhiyun uint32_t size, void *address)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun uint32_t i = 0;
30*4882a593Smuzhiyun uint8_t *data = address;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * writting FW_CFG_INVALID will cause read operation to resume at
34*4882a593Smuzhiyun * last offset, otherwise read will start at offset 0
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * Note: on platform where the control register is IO port, the
37*4882a593Smuzhiyun * endianness is little endian.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun if (entry != FW_CFG_INVALID)
40*4882a593Smuzhiyun outw(cpu_to_le16(entry), FW_CONTROL_PORT);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* the endianness of data register is string-preserving */
43*4882a593Smuzhiyun while (size--)
44*4882a593Smuzhiyun data[i++] = inb(FW_DATA_PORT);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
qemu_x86_fwcfg_read_entry_dma(struct fw_cfg_dma_access * dma)47*4882a593Smuzhiyun static void qemu_x86_fwcfg_read_entry_dma(struct fw_cfg_dma_access *dma)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun /* the DMA address register is big endian */
50*4882a593Smuzhiyun outl(cpu_to_be32((uintptr_t)dma), FW_DMA_PORT_HIGH);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun while (be32_to_cpu(dma->control) & ~FW_CFG_DMA_ERROR)
53*4882a593Smuzhiyun __asm__ __volatile__ ("pause");
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static struct fw_cfg_arch_ops fwcfg_x86_ops = {
57*4882a593Smuzhiyun .arch_read_pio = qemu_x86_fwcfg_read_entry_pio,
58*4882a593Smuzhiyun .arch_read_dma = qemu_x86_fwcfg_read_entry_dma
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun
enable_pm_piix(void)62*4882a593Smuzhiyun static void enable_pm_piix(void)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun u8 en;
65*4882a593Smuzhiyun u16 cmd;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* Set the PM I/O base */
68*4882a593Smuzhiyun pci_write_config32(PIIX_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Enable access to the PM I/O space */
71*4882a593Smuzhiyun pci_read_config16(PIIX_PM, PCI_COMMAND, &cmd);
72*4882a593Smuzhiyun cmd |= PCI_COMMAND_IO;
73*4882a593Smuzhiyun pci_write_config16(PIIX_PM, PCI_COMMAND, cmd);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* PM I/O Space Enable (PMIOSE) */
76*4882a593Smuzhiyun pci_read_config8(PIIX_PM, PMREGMISC, &en);
77*4882a593Smuzhiyun en |= PMIOSE;
78*4882a593Smuzhiyun pci_write_config8(PIIX_PM, PMREGMISC, en);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
enable_pm_ich9(void)81*4882a593Smuzhiyun static void enable_pm_ich9(void)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun /* Set the PM I/O base */
84*4882a593Smuzhiyun pci_write_config32(ICH9_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
qemu_chipset_init(void)87*4882a593Smuzhiyun static void qemu_chipset_init(void)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun u16 device, xbcs;
90*4882a593Smuzhiyun int pam, i;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * i440FX and Q35 chipset have different PAM register offset, but with
94*4882a593Smuzhiyun * the same bitfield layout. Here we determine the offset based on its
95*4882a593Smuzhiyun * PCI device ID.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun pci_read_config16(PCI_BDF(0, 0, 0), PCI_DEVICE_ID, &device);
98*4882a593Smuzhiyun i440fx = (device == PCI_DEVICE_ID_INTEL_82441);
99*4882a593Smuzhiyun pam = i440fx ? I440FX_PAM : Q35_PAM;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * Initialize Programmable Attribute Map (PAM) Registers
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * Configure legacy segments C/D/E/F to system RAM
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun for (i = 0; i < PAM_NUM; i++)
107*4882a593Smuzhiyun pci_write_config8(PCI_BDF(0, 0, 0), pam + i, PAM_RW);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (i440fx) {
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * Enable legacy IDE I/O ports decode
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * Note: QEMU always decode legacy IDE I/O port on PIIX chipset.
114*4882a593Smuzhiyun * However Linux ata_piix driver does sanity check on these two
115*4882a593Smuzhiyun * registers to see whether legacy ports decode is turned on.
116*4882a593Smuzhiyun * This is to make Linux ata_piix driver happy.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun pci_write_config16(PIIX_IDE, IDE0_TIM, IDE_DECODE_EN);
119*4882a593Smuzhiyun pci_write_config16(PIIX_IDE, IDE1_TIM, IDE_DECODE_EN);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* Enable I/O APIC */
122*4882a593Smuzhiyun pci_read_config16(PIIX_ISA, XBCS, &xbcs);
123*4882a593Smuzhiyun xbcs |= APIC_EN;
124*4882a593Smuzhiyun pci_write_config16(PIIX_ISA, XBCS, xbcs);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun enable_pm_piix();
127*4882a593Smuzhiyun } else {
128*4882a593Smuzhiyun /* Configure PCIe ECAM base address */
129*4882a593Smuzhiyun pci_write_config32(PCI_BDF(0, 0, 0), PCIEX_BAR,
130*4882a593Smuzhiyun CONFIG_PCIE_ECAM_BASE | BAR_EN);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun enable_pm_ich9();
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun #ifdef CONFIG_QFW
136*4882a593Smuzhiyun qemu_fwcfg_init(&fwcfg_x86_ops);
137*4882a593Smuzhiyun #endif
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(SPL_X86_32BIT_INIT)
arch_cpu_init(void)141*4882a593Smuzhiyun int arch_cpu_init(void)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun post_code(POST_CPU_INIT);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun return x86_cpu_init_f();
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(EFI_STUB) && \
150*4882a593Smuzhiyun !CONFIG_IS_ENABLED(SPL_X86_32BIT_INIT)
151*4882a593Smuzhiyun
checkcpu(void)152*4882a593Smuzhiyun int checkcpu(void)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun return 0;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
print_cpuinfo(void)157*4882a593Smuzhiyun int print_cpuinfo(void)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun post_code(POST_CPU_INFO);
160*4882a593Smuzhiyun return default_print_cpuinfo();
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun #endif
163*4882a593Smuzhiyun
reset_cpu(ulong addr)164*4882a593Smuzhiyun void reset_cpu(ulong addr)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun /* cold reset */
167*4882a593Smuzhiyun x86_full_reset();
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
arch_early_init_r(void)170*4882a593Smuzhiyun int arch_early_init_r(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun qemu_chipset_init();
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun return 0;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun #ifdef CONFIG_GENERATE_MP_TABLE
mp_determine_pci_dstirq(int bus,int dev,int func,int pirq)178*4882a593Smuzhiyun int mp_determine_pci_dstirq(int bus, int dev, int func, int pirq)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun u8 irq;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun if (i440fx) {
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * Not like most x86 platforms, the PIRQ[A-D] on PIIX3 are not
185*4882a593Smuzhiyun * connected to I/O APIC INTPIN#16-19. Instead they are routed
186*4882a593Smuzhiyun * to an irq number controled by the PIRQ routing register.
187*4882a593Smuzhiyun */
188*4882a593Smuzhiyun pci_read_config8(PCI_BDF(bus, dev, func),
189*4882a593Smuzhiyun PCI_INTERRUPT_LINE, &irq);
190*4882a593Smuzhiyun } else {
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * ICH9's PIRQ[A-H] are not consecutive numbers from 0 to 7.
193*4882a593Smuzhiyun * PIRQ[A-D] still maps to [0-3] but PIRQ[E-H] maps to [8-11].
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun irq = pirq < 8 ? pirq + 16 : pirq + 12;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun return irq;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun #endif
201