Simon Glass | 99c1904 | 2019-12-08 17:40:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <dt-structs.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 99c1904 | 2019-12-08 17:40:07 -0700 | [diff] [blame] | 10 | #include <spl.h> |
| 11 | #include <asm/intel_pinctrl.h> |
| 12 | #include <asm/intel_regs.h> |
| 13 | #include <asm/pci.h> |
| 14 | #include <asm/arch/systemagent.h> |
| 15 | |
| 16 | /** |
| 17 | * struct apl_hostbridge_platdata - platform data for hostbridge |
| 18 | * |
| 19 | * @dtplat: Platform data for of-platdata |
| 20 | * @early_pads: Early pad data to set up, each (pad, cfg0, cfg1) |
| 21 | * @early_pads_count: Number of pads to process |
| 22 | * @pciex_region_size: BAR length in bytes |
| 23 | * @bdf: Bus/device/function of hostbridge |
| 24 | */ |
| 25 | struct apl_hostbridge_platdata { |
| 26 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 27 | struct dtd_intel_apl_hostbridge dtplat; |
| 28 | #endif |
| 29 | u32 *early_pads; |
| 30 | int early_pads_count; |
| 31 | uint pciex_region_size; |
| 32 | pci_dev_t bdf; |
| 33 | }; |
| 34 | |
| 35 | enum { |
| 36 | PCIEXBAR = 0x60, |
| 37 | PCIEXBAR_LENGTH_256MB = 0, |
| 38 | PCIEXBAR_LENGTH_128MB, |
| 39 | PCIEXBAR_LENGTH_64MB, |
| 40 | |
| 41 | PCIEXBAR_PCIEXBAREN = 1 << 0, |
| 42 | |
Simon Glass | a18e96e | 2020-09-22 12:45:17 -0600 | [diff] [blame^] | 43 | BGSM = 0xb4, /* Base GTT Stolen Memory */ |
Simon Glass | 99c1904 | 2019-12-08 17:40:07 -0700 | [diff] [blame] | 44 | TSEG = 0xb8, /* TSEG base */ |
Simon Glass | a18e96e | 2020-09-22 12:45:17 -0600 | [diff] [blame^] | 45 | TOLUD = 0xbc, |
Simon Glass | 99c1904 | 2019-12-08 17:40:07 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static int apl_hostbridge_early_init_pinctrl(struct udevice *dev) |
| 49 | { |
| 50 | struct apl_hostbridge_platdata *plat = dev_get_platdata(dev); |
| 51 | struct udevice *pinctrl; |
| 52 | int ret; |
| 53 | |
| 54 | ret = uclass_first_device_err(UCLASS_PINCTRL, &pinctrl); |
| 55 | if (ret) |
| 56 | return log_msg_ret("no hostbridge pinctrl", ret); |
| 57 | |
| 58 | return pinctrl_config_pads(pinctrl, plat->early_pads, |
| 59 | plat->early_pads_count); |
| 60 | } |
| 61 | |
| 62 | static int apl_hostbridge_early_init(struct udevice *dev) |
| 63 | { |
| 64 | struct apl_hostbridge_platdata *plat = dev_get_platdata(dev); |
| 65 | u32 region_size; |
| 66 | ulong base; |
| 67 | u32 reg; |
| 68 | int ret; |
| 69 | |
| 70 | /* Set up the MCHBAR */ |
| 71 | pci_x86_read_config(plat->bdf, MCHBAR, &base, PCI_SIZE_32); |
| 72 | base = MCH_BASE_ADDRESS; |
| 73 | pci_x86_write_config(plat->bdf, MCHBAR, base | 1, PCI_SIZE_32); |
| 74 | |
| 75 | /* |
| 76 | * The PCIEXBAR is assumed to live in the memory mapped IO space under |
| 77 | * 4GiB |
| 78 | */ |
| 79 | pci_x86_write_config(plat->bdf, PCIEXBAR + 4, 0, PCI_SIZE_32); |
| 80 | |
| 81 | switch (plat->pciex_region_size >> 20) { |
| 82 | default: |
| 83 | case 256: |
| 84 | region_size = PCIEXBAR_LENGTH_256MB; |
| 85 | break; |
| 86 | case 128: |
| 87 | region_size = PCIEXBAR_LENGTH_128MB; |
| 88 | break; |
| 89 | case 64: |
| 90 | region_size = PCIEXBAR_LENGTH_64MB; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | reg = CONFIG_MMCONF_BASE_ADDRESS | (region_size << 1) |
| 95 | | PCIEXBAR_PCIEXBAREN; |
| 96 | pci_x86_write_config(plat->bdf, PCIEXBAR, reg, PCI_SIZE_32); |
| 97 | |
| 98 | /* |
| 99 | * TSEG defines the base of SMM range. BIOS determines the base |
| 100 | * of TSEG memory which must be at or below Graphics base of GTT |
| 101 | * Stolen memory, hence its better to clear TSEG register early |
| 102 | * to avoid power on default non-zero value (if any). |
| 103 | */ |
| 104 | pci_x86_write_config(plat->bdf, TSEG, 0, PCI_SIZE_32); |
| 105 | |
| 106 | ret = apl_hostbridge_early_init_pinctrl(dev); |
| 107 | if (ret) |
| 108 | return log_msg_ret("pinctrl", ret); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int apl_hostbridge_ofdata_to_platdata(struct udevice *dev) |
| 114 | { |
| 115 | struct apl_hostbridge_platdata *plat = dev_get_platdata(dev); |
| 116 | struct udevice *pinctrl; |
| 117 | int ret; |
| 118 | |
| 119 | /* |
| 120 | * The host bridge holds the early pad data needed to get through TPL. |
| 121 | * This is a small amount of data, enough to fit in TPL, so we keep it |
| 122 | * separate from the full pad data, stored in the fsp-s subnode. That |
| 123 | * subnode is not present in TPL, to save space. |
| 124 | */ |
| 125 | ret = uclass_first_device_err(UCLASS_PINCTRL, &pinctrl); |
| 126 | if (ret) |
| 127 | return log_msg_ret("no hostbridge PINCTRL", ret); |
| 128 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 129 | int root; |
| 130 | |
| 131 | /* Get length of PCI Express Region */ |
| 132 | plat->pciex_region_size = dev_read_u32_default(dev, "pciex-region-size", |
| 133 | 256 << 20); |
| 134 | |
| 135 | root = pci_get_devfn(dev); |
| 136 | if (root < 0) |
| 137 | return log_msg_ret("Cannot get host-bridge PCI address", root); |
| 138 | plat->bdf = root; |
| 139 | |
| 140 | ret = pinctrl_read_pads(pinctrl, dev_ofnode(dev), "early-pads", |
| 141 | &plat->early_pads, &plat->early_pads_count); |
| 142 | if (ret) |
| 143 | return log_msg_ret("early-pads", ret); |
| 144 | #else |
| 145 | struct dtd_intel_apl_hostbridge *dtplat = &plat->dtplat; |
| 146 | int size; |
| 147 | |
| 148 | plat->pciex_region_size = dtplat->pciex_region_size; |
| 149 | plat->bdf = pci_ofplat_get_devfn(dtplat->reg[0]); |
| 150 | |
| 151 | /* Assume that if everything is 0, it is empty */ |
| 152 | plat->early_pads = dtplat->early_pads; |
| 153 | size = ARRAY_SIZE(dtplat->early_pads); |
| 154 | plat->early_pads_count = pinctrl_count_pads(pinctrl, plat->early_pads, |
| 155 | size); |
| 156 | |
| 157 | #endif |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int apl_hostbridge_probe(struct udevice *dev) |
| 163 | { |
| 164 | if (spl_phase() == PHASE_TPL) |
| 165 | return apl_hostbridge_early_init(dev); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Simon Glass | a18e96e | 2020-09-22 12:45:17 -0600 | [diff] [blame^] | 170 | static ulong sa_read_reg(struct udevice *dev, int reg) |
| 171 | { |
| 172 | u32 val; |
| 173 | |
| 174 | /* All regions concerned for have 1 MiB alignment */ |
| 175 | dm_pci_read_config32(dev, BGSM, &val); |
| 176 | |
| 177 | return ALIGN_DOWN(val, 1 << 20); |
| 178 | } |
| 179 | |
| 180 | ulong sa_get_tolud_base(struct udevice *dev) |
| 181 | { |
| 182 | return sa_read_reg(dev, TOLUD); |
| 183 | } |
| 184 | |
| 185 | ulong sa_get_gsm_base(struct udevice *dev) |
| 186 | { |
| 187 | return sa_read_reg(dev, BGSM); |
| 188 | } |
| 189 | |
| 190 | ulong sa_get_tseg_base(struct udevice *dev) |
| 191 | { |
| 192 | return sa_read_reg(dev, TSEG); |
| 193 | } |
| 194 | |
Simon Glass | 99c1904 | 2019-12-08 17:40:07 -0700 | [diff] [blame] | 195 | static const struct udevice_id apl_hostbridge_ids[] = { |
| 196 | { .compatible = "intel,apl-hostbridge" }, |
| 197 | { } |
| 198 | }; |
| 199 | |
| 200 | U_BOOT_DRIVER(apl_hostbridge_drv) = { |
| 201 | .name = "intel_apl_hostbridge", |
| 202 | .id = UCLASS_NORTHBRIDGE, |
| 203 | .of_match = apl_hostbridge_ids, |
| 204 | .ofdata_to_platdata = apl_hostbridge_ofdata_to_platdata, |
| 205 | .probe = apl_hostbridge_probe, |
| 206 | .platdata_auto_alloc_size = sizeof(struct apl_hostbridge_platdata), |
| 207 | }; |