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