blob: 7ad0ee49a19a7c4eb90959db9018e1362d4de8fe [file] [log] [blame]
Bin Menga65b25d2015-05-07 21:34:08 +08001/*
2 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Bin Meng60392002016-02-01 01:40:56 -08008#include <pci.h>
Bin Meng5c564222015-06-03 09:20:06 +08009#include <asm/irq.h>
Bin Menga65b25d2015-05-07 21:34:08 +080010#include <asm/post.h>
11#include <asm/processor.h>
Bin Meng48748592015-11-06 02:04:49 -080012#include <asm/arch/device.h>
13#include <asm/arch/qemu.h>
Miao Yanf60df202016-01-07 01:32:00 -080014#include <asm/fw_cfg.h>
Bin Meng48748592015-11-06 02:04:49 -080015
16static bool i440fx;
17
Miao Yana3b15a02016-01-20 01:57:05 -080018static void enable_pm_piix(void)
19{
20 u8 en;
21 u16 cmd;
22
23 /* Set the PM I/O base */
Bin Meng60392002016-02-01 01:40:56 -080024 pci_write_config32(PIIX_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
Miao Yana3b15a02016-01-20 01:57:05 -080025
26 /* Enable access to the PM I/O space */
Bin Meng60392002016-02-01 01:40:56 -080027 pci_read_config16(PIIX_PM, PCI_COMMAND, &cmd);
Miao Yana3b15a02016-01-20 01:57:05 -080028 cmd |= PCI_COMMAND_IO;
Bin Meng60392002016-02-01 01:40:56 -080029 pci_write_config16(PIIX_PM, PCI_COMMAND, cmd);
Miao Yana3b15a02016-01-20 01:57:05 -080030
31 /* PM I/O Space Enable (PMIOSE) */
Bin Meng60392002016-02-01 01:40:56 -080032 pci_read_config8(PIIX_PM, PMREGMISC, &en);
Miao Yana3b15a02016-01-20 01:57:05 -080033 en |= PMIOSE;
Bin Meng60392002016-02-01 01:40:56 -080034 pci_write_config8(PIIX_PM, PMREGMISC, en);
Miao Yana3b15a02016-01-20 01:57:05 -080035}
36
37static void enable_pm_ich9(void)
38{
39 /* Set the PM I/O base */
Bin Meng60392002016-02-01 01:40:56 -080040 pci_write_config32(ICH9_PM, PMBA, CONFIG_ACPI_PM1_BASE | 1);
Miao Yana3b15a02016-01-20 01:57:05 -080041}
42
Bin Meng48748592015-11-06 02:04:49 -080043static void qemu_chipset_init(void)
44{
45 u16 device, xbcs;
46 int pam, i;
47
48 /*
49 * i440FX and Q35 chipset have different PAM register offset, but with
50 * the same bitfield layout. Here we determine the offset based on its
51 * PCI device ID.
52 */
Bin Meng60392002016-02-01 01:40:56 -080053 pci_read_config16(PCI_BDF(0, 0, 0), PCI_DEVICE_ID, &device);
Bin Meng48748592015-11-06 02:04:49 -080054 i440fx = (device == PCI_DEVICE_ID_INTEL_82441);
55 pam = i440fx ? I440FX_PAM : Q35_PAM;
56
57 /*
58 * Initialize Programmable Attribute Map (PAM) Registers
59 *
60 * Configure legacy segments C/D/E/F to system RAM
61 */
62 for (i = 0; i < PAM_NUM; i++)
Bin Meng60392002016-02-01 01:40:56 -080063 pci_write_config8(PCI_BDF(0, 0, 0), pam + i, PAM_RW);
Bin Meng48748592015-11-06 02:04:49 -080064
65 if (i440fx) {
66 /*
67 * Enable legacy IDE I/O ports decode
68 *
69 * Note: QEMU always decode legacy IDE I/O port on PIIX chipset.
70 * However Linux ata_piix driver does sanity check on these two
71 * registers to see whether legacy ports decode is turned on.
72 * This is to make Linux ata_piix driver happy.
73 */
Bin Meng60392002016-02-01 01:40:56 -080074 pci_write_config16(PIIX_IDE, IDE0_TIM, IDE_DECODE_EN);
75 pci_write_config16(PIIX_IDE, IDE1_TIM, IDE_DECODE_EN);
Bin Meng48748592015-11-06 02:04:49 -080076
77 /* Enable I/O APIC */
Bin Meng60392002016-02-01 01:40:56 -080078 pci_read_config16(PIIX_ISA, XBCS, &xbcs);
Bin Meng48748592015-11-06 02:04:49 -080079 xbcs |= APIC_EN;
Bin Meng60392002016-02-01 01:40:56 -080080 pci_write_config16(PIIX_ISA, XBCS, xbcs);
Miao Yana3b15a02016-01-20 01:57:05 -080081
82 enable_pm_piix();
Bin Meng48748592015-11-06 02:04:49 -080083 } else {
84 /* Configure PCIe ECAM base address */
Bin Meng60392002016-02-01 01:40:56 -080085 pci_write_config32(PCI_BDF(0, 0, 0), PCIEX_BAR,
86 CONFIG_PCIE_ECAM_BASE | BAR_EN);
Miao Yana3b15a02016-01-20 01:57:05 -080087
88 enable_pm_ich9();
Bin Meng48748592015-11-06 02:04:49 -080089 }
Miao Yanf60df202016-01-07 01:32:00 -080090
91 qemu_fwcfg_init();
Bin Meng48748592015-11-06 02:04:49 -080092}
Bin Menga65b25d2015-05-07 21:34:08 +080093
94int arch_cpu_init(void)
95{
96 int ret;
97
98 post_code(POST_CPU_INIT);
Bin Menga65b25d2015-05-07 21:34:08 +080099
100 ret = x86_cpu_init_f();
101 if (ret)
102 return ret;
103
104 return 0;
105}
106
Simon Glasseeae5102015-08-04 12:34:03 -0600107#ifndef CONFIG_EFI_STUB
Bin Menga65b25d2015-05-07 21:34:08 +0800108int print_cpuinfo(void)
109{
110 post_code(POST_CPU_INFO);
111 return default_print_cpuinfo();
112}
Simon Glasseeae5102015-08-04 12:34:03 -0600113#endif
Bin Menga65b25d2015-05-07 21:34:08 +0800114
115void reset_cpu(ulong addr)
116{
117 /* cold reset */
118 x86_full_reset();
119}
Bin Meng5c564222015-06-03 09:20:06 +0800120
Bin Meng48748592015-11-06 02:04:49 -0800121int arch_early_init_r(void)
122{
123 qemu_chipset_init();
124
125 return 0;
126}
127
Bin Meng48748592015-11-06 02:04:49 -0800128#ifdef CONFIG_GENERATE_MP_TABLE
129int mp_determine_pci_dstirq(int bus, int dev, int func, int pirq)
130{
131 u8 irq;
132
133 if (i440fx) {
134 /*
135 * Not like most x86 platforms, the PIRQ[A-D] on PIIX3 are not
136 * connected to I/O APIC INTPIN#16-19. Instead they are routed
137 * to an irq number controled by the PIRQ routing register.
138 */
Bin Meng60392002016-02-01 01:40:56 -0800139 pci_read_config8(PCI_BDF(bus, dev, func),
140 PCI_INTERRUPT_LINE, &irq);
Bin Meng48748592015-11-06 02:04:49 -0800141 } else {
142 /*
143 * ICH9's PIRQ[A-H] are not consecutive numbers from 0 to 7.
144 * PIRQ[A-D] still maps to [0-3] but PIRQ[E-H] maps to [8-11].
145 */
146 irq = pirq < 8 ? pirq + 16 : pirq + 12;
147 }
148
149 return irq;
150}
151#endif