blob: 5e8b4f068e1f5ecdc3dfb5d52ec791e8f40f619d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bin Meng2229c4c2015-05-07 21:34:08 +08002/*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
Bin Meng2229c4c2015-05-07 21:34:08 +08004 */
5
6#include <common.h>
Bin Meng5fecada2016-02-01 01:40:56 -08007#include <pci.h>
Miao Yan92106272016-05-22 19:37:17 -07008#include <qfw.h>
Bin Mengef37e7b2015-06-03 09:20:06 +08009#include <asm/irq.h>
Bin Meng2229c4c2015-05-07 21:34:08 +080010#include <asm/post.h>
11#include <asm/processor.h>
Bin Menge456f2b2015-11-06 02:04:49 -080012#include <asm/arch/device.h>
13#include <asm/arch/qemu.h>
14
15static bool i440fx;
16
Miao Yan8a153832016-05-22 19:37:15 -070017#ifdef CONFIG_QFW
18
Miao Yanf3c6a4e2016-05-22 19:37:16 -070019/* on x86, the qfw registers are all IO ports */
Miao Yan8a153832016-05-22 19:37:15 -070020#define FW_CONTROL_PORT 0x510
21#define FW_DATA_PORT 0x511
22#define FW_DMA_PORT_LOW 0x514
23#define FW_DMA_PORT_HIGH 0x518
24
25static void qemu_x86_fwcfg_read_entry_pio(uint16_t entry,
26 uint32_t size, void *address)
27{
28 uint32_t i = 0;
29 uint8_t *data = address;
30
31 /*
32 * writting FW_CFG_INVALID will cause read operation to resume at
33 * last offset, otherwise read will start at offset 0
Miao Yanf3c6a4e2016-05-22 19:37:16 -070034 *
35 * Note: on platform where the control register is IO port, the
36 * endianness is little endian.
Miao Yan8a153832016-05-22 19:37:15 -070037 */
38 if (entry != FW_CFG_INVALID)
Miao Yanf3c6a4e2016-05-22 19:37:16 -070039 outw(cpu_to_le16(entry), FW_CONTROL_PORT);
40
41 /* the endianness of data register is string-preserving */
Miao Yan8a153832016-05-22 19:37:15 -070042 while (size--)
43 data[i++] = inb(FW_DATA_PORT);
44}
45
46static void qemu_x86_fwcfg_read_entry_dma(struct fw_cfg_dma_access *dma)
47{
Miao Yanf3c6a4e2016-05-22 19:37:16 -070048 /* the DMA address register is big endian */
Bin Meng95e4a392017-01-18 03:32:56 -080049 outl(cpu_to_be32((uintptr_t)dma), FW_DMA_PORT_HIGH);
Miao Yan8a153832016-05-22 19:37:15 -070050
51 while (be32_to_cpu(dma->control) & ~FW_CFG_DMA_ERROR)
52 __asm__ __volatile__ ("pause");
53}
54
55static struct fw_cfg_arch_ops fwcfg_x86_ops = {
56 .arch_read_pio = qemu_x86_fwcfg_read_entry_pio,
57 .arch_read_dma = qemu_x86_fwcfg_read_entry_dma
58};
59#endif
60
Miao Yan35603ff2016-01-20 01:57:05 -080061static void enable_pm_piix(void)
62{
63 u8 en;
64 u16 cmd;
65
66 /* Set the PM I/O base */
Bin Meng5fecada2016-02-01 01:40:56 -080067 pci_write_config32(PIIX_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
Miao Yan35603ff2016-01-20 01:57:05 -080068
69 /* Enable access to the PM I/O space */
Bin Meng5fecada2016-02-01 01:40:56 -080070 pci_read_config16(PIIX_PM, PCI_COMMAND, &cmd);
Miao Yan35603ff2016-01-20 01:57:05 -080071 cmd |= PCI_COMMAND_IO;
Bin Meng5fecada2016-02-01 01:40:56 -080072 pci_write_config16(PIIX_PM, PCI_COMMAND, cmd);
Miao Yan35603ff2016-01-20 01:57:05 -080073
74 /* PM I/O Space Enable (PMIOSE) */
Bin Meng5fecada2016-02-01 01:40:56 -080075 pci_read_config8(PIIX_PM, PMREGMISC, &en);
Miao Yan35603ff2016-01-20 01:57:05 -080076 en |= PMIOSE;
Bin Meng5fecada2016-02-01 01:40:56 -080077 pci_write_config8(PIIX_PM, PMREGMISC, en);
Miao Yan35603ff2016-01-20 01:57:05 -080078}
79
80static void enable_pm_ich9(void)
81{
82 /* Set the PM I/O base */
Bin Meng5fecada2016-02-01 01:40:56 -080083 pci_write_config32(ICH9_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
Miao Yan35603ff2016-01-20 01:57:05 -080084}
85
Bin Menge456f2b2015-11-06 02:04:49 -080086static void qemu_chipset_init(void)
87{
88 u16 device, xbcs;
89 int pam, i;
90
91 /*
92 * i440FX and Q35 chipset have different PAM register offset, but with
93 * the same bitfield layout. Here we determine the offset based on its
94 * PCI device ID.
95 */
Bin Meng5fecada2016-02-01 01:40:56 -080096 pci_read_config16(PCI_BDF(0, 0, 0), PCI_DEVICE_ID, &device);
Bin Menge456f2b2015-11-06 02:04:49 -080097 i440fx = (device == PCI_DEVICE_ID_INTEL_82441);
98 pam = i440fx ? I440FX_PAM : Q35_PAM;
99
100 /*
101 * Initialize Programmable Attribute Map (PAM) Registers
102 *
103 * Configure legacy segments C/D/E/F to system RAM
104 */
105 for (i = 0; i < PAM_NUM; i++)
Bin Meng5fecada2016-02-01 01:40:56 -0800106 pci_write_config8(PCI_BDF(0, 0, 0), pam + i, PAM_RW);
Bin Menge456f2b2015-11-06 02:04:49 -0800107
108 if (i440fx) {
109 /*
110 * Enable legacy IDE I/O ports decode
111 *
112 * Note: QEMU always decode legacy IDE I/O port on PIIX chipset.
113 * However Linux ata_piix driver does sanity check on these two
114 * registers to see whether legacy ports decode is turned on.
115 * This is to make Linux ata_piix driver happy.
116 */
Bin Meng5fecada2016-02-01 01:40:56 -0800117 pci_write_config16(PIIX_IDE, IDE0_TIM, IDE_DECODE_EN);
118 pci_write_config16(PIIX_IDE, IDE1_TIM, IDE_DECODE_EN);
Bin Menge456f2b2015-11-06 02:04:49 -0800119
120 /* Enable I/O APIC */
Bin Meng5fecada2016-02-01 01:40:56 -0800121 pci_read_config16(PIIX_ISA, XBCS, &xbcs);
Bin Menge456f2b2015-11-06 02:04:49 -0800122 xbcs |= APIC_EN;
Bin Meng5fecada2016-02-01 01:40:56 -0800123 pci_write_config16(PIIX_ISA, XBCS, xbcs);
Miao Yan35603ff2016-01-20 01:57:05 -0800124
125 enable_pm_piix();
Bin Menge456f2b2015-11-06 02:04:49 -0800126 } else {
127 /* Configure PCIe ECAM base address */
Bin Meng5fecada2016-02-01 01:40:56 -0800128 pci_write_config32(PCI_BDF(0, 0, 0), PCIEX_BAR,
129 CONFIG_PCIE_ECAM_BASE | BAR_EN);
Miao Yan35603ff2016-01-20 01:57:05 -0800130
131 enable_pm_ich9();
Bin Menge456f2b2015-11-06 02:04:49 -0800132 }
Miao Yan58a3ce22016-01-07 01:32:00 -0800133
Miao Yan4fcd7f22016-05-22 19:37:14 -0700134#ifdef CONFIG_QFW
Miao Yan8a153832016-05-22 19:37:15 -0700135 qemu_fwcfg_init(&fwcfg_x86_ops);
Miao Yan4fcd7f22016-05-22 19:37:14 -0700136#endif
Bin Menge456f2b2015-11-06 02:04:49 -0800137}
Bin Meng2229c4c2015-05-07 21:34:08 +0800138
Bin Meng7172c0a2017-01-18 03:32:55 -0800139#if !CONFIG_IS_ENABLED(SPL_X86_32BIT_INIT)
Bin Meng2229c4c2015-05-07 21:34:08 +0800140int arch_cpu_init(void)
141{
Bin Meng2229c4c2015-05-07 21:34:08 +0800142 post_code(POST_CPU_INIT);
Bin Meng2229c4c2015-05-07 21:34:08 +0800143
Masahiro Yamada17103212016-09-06 22:17:36 +0900144 return x86_cpu_init_f();
Bin Meng2229c4c2015-05-07 21:34:08 +0800145}
Simon Glassee7c36f2017-03-28 10:27:30 -0600146
147int checkcpu(void)
148{
149 return 0;
150}
151
Bin Meng2229c4c2015-05-07 21:34:08 +0800152int print_cpuinfo(void)
153{
154 post_code(POST_CPU_INFO);
155 return default_print_cpuinfo();
156}
Simon Glass752f9762015-08-04 12:34:03 -0600157#endif
Bin Meng2229c4c2015-05-07 21:34:08 +0800158
Bin Menge456f2b2015-11-06 02:04:49 -0800159int arch_early_init_r(void)
160{
161 qemu_chipset_init();
162
163 return 0;
164}
165
Bin Menge456f2b2015-11-06 02:04:49 -0800166#ifdef CONFIG_GENERATE_MP_TABLE
167int mp_determine_pci_dstirq(int bus, int dev, int func, int pirq)
168{
169 u8 irq;
170
171 if (i440fx) {
172 /*
173 * Not like most x86 platforms, the PIRQ[A-D] on PIIX3 are not
174 * connected to I/O APIC INTPIN#16-19. Instead they are routed
175 * to an irq number controled by the PIRQ routing register.
176 */
Bin Meng5fecada2016-02-01 01:40:56 -0800177 pci_read_config8(PCI_BDF(bus, dev, func),
178 PCI_INTERRUPT_LINE, &irq);
Bin Menge456f2b2015-11-06 02:04:49 -0800179 } else {
180 /*
181 * ICH9's PIRQ[A-H] are not consecutive numbers from 0 to 7.
182 * PIRQ[A-D] still maps to [0-3] but PIRQ[E-H] maps to [8-11].
183 */
184 irq = pirq < 8 ? pirq + 16 : pirq + 12;
185 }
186
187 return irq;
188}
189#endif