Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <commonlib/stdlib.h> |
| 4 | #include <console/console.h> |
| 5 | #include <device/device.h> |
| 6 | #include <device/mmio.h> |
| 7 | #include <device/pci.h> |
| 8 | #include <device/pci_def.h> |
| 9 | #include <device/pci_ids.h> |
| 10 | #include <device/resource.h> |
| 11 | #include <delay.h> |
| 12 | #include <lib.h> |
| 13 | #include <soc/addressmap.h> |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 14 | #include <soc/early_init.h> |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 15 | #include <soc/pcie.h> |
| 16 | #include <soc/pcie_common.h> |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 17 | #include <soc/soc_chip.h> |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <types.h> |
| 20 | |
| 21 | #define PCIE_SETTING_REG 0x80 |
| 22 | #define PCIE_PCI_IDS_1 0x9c |
| 23 | #define PCI_CLASS(class) ((class) << 8) |
| 24 | #define PCIE_RC_MODE BIT(0) |
| 25 | |
| 26 | #define PCIE_CFGNUM_REG 0x140 |
| 27 | #define PCIE_CFG_DEVFN(devfn) ((devfn) & GENMASK(7, 0)) |
| 28 | #define PCIE_CFG_BUS(bus) (((bus) << 8) & GENMASK(15, 8)) |
| 29 | #define PCIE_CFG_OFFSET_ADDR 0x1000 |
| 30 | #define PCIE_CFG_HEADER(bus, devfn) \ |
| 31 | (PCIE_CFG_BUS(bus) | PCIE_CFG_DEVFN(devfn)) |
| 32 | |
| 33 | #define PCIE_RST_CTRL_REG 0x148 |
| 34 | #define PCIE_MAC_RSTB BIT(0) |
| 35 | #define PCIE_PHY_RSTB BIT(1) |
| 36 | #define PCIE_BRG_RSTB BIT(2) |
| 37 | #define PCIE_PE_RSTB BIT(3) |
| 38 | |
| 39 | #define PCIE_LTSSM_STATUS_REG 0x150 |
| 40 | #define PCIE_LTSSM_STATE(val) (((val) >> 24) & 0x1f) |
| 41 | |
| 42 | #define PCIE_LINK_STATUS_REG 0x154 |
| 43 | #define PCIE_CTRL_LINKUP BIT(8) |
| 44 | |
| 45 | #define PCI_NUM_INTX 4 |
| 46 | #define PCIE_INT_ENABLE_REG 0x180 |
| 47 | #define PCIE_INTX_SHIFT 24 |
| 48 | #define PCIE_INTX_ENABLE \ |
| 49 | GENMASK(PCIE_INTX_SHIFT + PCI_NUM_INTX - 1, PCIE_INTX_SHIFT) |
| 50 | |
| 51 | #define PCIE_TRANS_TABLE_BASE_REG 0x800 |
| 52 | #define PCIE_ATR_SRC_ADDR_MSB_OFFSET 0x4 |
| 53 | #define PCIE_ATR_TRSL_ADDR_LSB_OFFSET 0x8 |
| 54 | #define PCIE_ATR_TRSL_ADDR_MSB_OFFSET 0xc |
| 55 | #define PCIE_ATR_TRSL_PARAM_OFFSET 0x10 |
| 56 | #define PCIE_ATR_TLB_SET_OFFSET 0x20 |
| 57 | |
| 58 | #define PCIE_MAX_TRANS_TABLES 8 |
| 59 | #define PCIE_ATR_EN BIT(0) |
| 60 | #define PCIE_ATR_SIZE(size) \ |
| 61 | (((((size) - 1) << 1) & GENMASK(6, 1)) | PCIE_ATR_EN) |
| 62 | #define PCIE_ATR_ID(id) ((id) & GENMASK(3, 0)) |
| 63 | #define PCIE_ATR_TYPE_MEM PCIE_ATR_ID(0) |
| 64 | #define PCIE_ATR_TYPE_IO PCIE_ATR_ID(1) |
| 65 | #define PCIE_ATR_TLP_TYPE(type) (((type) << 16) & GENMASK(18, 16)) |
| 66 | #define PCIE_ATR_TLP_TYPE_MEM PCIE_ATR_TLP_TYPE(0) |
| 67 | #define PCIE_ATR_TLP_TYPE_IO PCIE_ATR_TLP_TYPE(2) |
| 68 | |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 69 | /* LTSSM state in PCIE_LTSSM_STATUS_REG bit[28:24] */ |
| 70 | static const char *const ltssm_str[] = { |
| 71 | "detect.quiet", /* 0x00 */ |
| 72 | "detect.active", /* 0x01 */ |
| 73 | "polling.active", /* 0x02 */ |
| 74 | "polling.compliance", /* 0x03 */ |
| 75 | "polling.configuration", /* 0x04 */ |
| 76 | "config.linkwidthstart", /* 0x05 */ |
| 77 | "config.linkwidthaccept", /* 0x06 */ |
| 78 | "config.lanenumwait", /* 0x07 */ |
| 79 | "config.lanenumaccept", /* 0x08 */ |
| 80 | "config.complete", /* 0x09 */ |
| 81 | "config.idle", /* 0x0A */ |
| 82 | "recovery.receiverlock", /* 0x0B */ |
| 83 | "recovery.equalization", /* 0x0C */ |
| 84 | "recovery.speed", /* 0x0D */ |
| 85 | "recovery.receiverconfig", /* 0x0E */ |
| 86 | "recovery.idle", /* 0x0F */ |
| 87 | "L0", /* 0x10 */ |
| 88 | "L0s", /* 0x11 */ |
| 89 | "L1.entry", /* 0x12 */ |
| 90 | "L1.idle", /* 0x13 */ |
| 91 | "L2.idle", /* 0x14 */ |
| 92 | "L2.transmitwake", /* 0x15 */ |
| 93 | "disable", /* 0x16 */ |
| 94 | "loopback.entry", /* 0x17 */ |
| 95 | "loopback.active", /* 0x18 */ |
| 96 | "loopback.exit", /* 0x19 */ |
| 97 | "hotreset", /* 0x1A */ |
| 98 | }; |
| 99 | |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 100 | static uintptr_t mtk_pcie_get_controller_base(pci_devfn_t devfn) |
| 101 | { |
| 102 | struct device *root_dev; |
| 103 | const mtk_soc_config_t *config; |
| 104 | static uintptr_t base = 0; |
| 105 | |
| 106 | if (!base) { |
| 107 | root_dev = pcidev_path_on_root(devfn); |
| 108 | config = config_of(root_dev); |
| 109 | base = config->pcie_config.base; |
| 110 | } |
| 111 | |
| 112 | return base; |
| 113 | } |
| 114 | |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 115 | volatile union pci_bank *pci_map_bus(pci_devfn_t dev) |
| 116 | { |
| 117 | u32 val, devfn, bus; |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 118 | uintptr_t base; |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 119 | |
| 120 | devfn = PCI_DEV2DEVFN(dev); |
| 121 | bus = PCI_DEV2SEGBUS(dev); |
| 122 | val = PCIE_CFG_HEADER(bus, devfn); |
| 123 | |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 124 | base = mtk_pcie_get_controller_base(dev); |
| 125 | write32p(base + PCIE_CFGNUM_REG, val); |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 126 | |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 127 | return (void *)(base + PCIE_CFG_OFFSET_ADDR); |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int mtk_pcie_set_trans_window(struct device *dev, uintptr_t table, |
| 131 | const struct mtk_pcie_mmio_res *mmio_res) |
| 132 | { |
| 133 | const char *range_type; |
| 134 | uint32_t table_attr; |
| 135 | |
| 136 | if (!mmio_res) |
| 137 | return -1; |
| 138 | |
| 139 | if (mmio_res->type == IORESOURCE_IO) { |
| 140 | range_type = "IO"; |
| 141 | table_attr = PCIE_ATR_TYPE_IO | PCIE_ATR_TLP_TYPE_IO; |
| 142 | } else if (mmio_res->type == IORESOURCE_MEM) { |
| 143 | range_type = "MEM"; |
| 144 | table_attr = PCIE_ATR_TYPE_MEM | PCIE_ATR_TLP_TYPE_MEM; |
| 145 | } else { |
| 146 | printk(BIOS_ERR, "%s: Unknown trans table type %#lx\n", |
| 147 | __func__, mmio_res->type); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | write32p(table, mmio_res->cpu_addr | |
| 152 | PCIE_ATR_SIZE(__fls(mmio_res->size))); |
| 153 | write32p(table + PCIE_ATR_SRC_ADDR_MSB_OFFSET, 0); |
| 154 | write32p(table + PCIE_ATR_TRSL_ADDR_LSB_OFFSET, mmio_res->pci_addr); |
| 155 | write32p(table + PCIE_ATR_TRSL_ADDR_MSB_OFFSET, 0); |
| 156 | write32p(table + PCIE_ATR_TRSL_PARAM_OFFSET, table_attr); |
| 157 | |
| 158 | printk(BIOS_INFO, |
| 159 | "%s: set %s trans window: cpu_addr = %#x, pci_addr = %#x, size = %#x\n", |
| 160 | __func__, range_type, mmio_res->cpu_addr, mmio_res->pci_addr, |
| 161 | mmio_res->size); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void mtk_pcie_domain_new_res(struct device *dev, unsigned int index, |
| 167 | const struct mtk_pcie_mmio_res *mmio_res) |
| 168 | { |
| 169 | struct resource *res; |
| 170 | |
| 171 | if (!mmio_res) |
| 172 | return; |
| 173 | |
| 174 | res = new_resource(dev, index); |
| 175 | res->base = mmio_res->cpu_addr; |
| 176 | res->limit = mmio_res->cpu_addr + mmio_res->size - 1; |
| 177 | res->flags = mmio_res->type | IORESOURCE_SUBTRACTIVE | |
| 178 | IORESOURCE_ASSIGNED; |
| 179 | } |
| 180 | |
| 181 | void mtk_pcie_domain_read_resources(struct device *dev) |
| 182 | { |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 183 | const mtk_soc_config_t *config = config_of(dev); |
| 184 | const struct mtk_pcie_config *conf = &config->pcie_config; |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 185 | |
| 186 | mtk_pcie_domain_new_res(dev, IOINDEX_SUBTRACTIVE(0, 0), |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 187 | &conf->mmio_res_io); |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 188 | |
| 189 | mtk_pcie_domain_new_res(dev, IOINDEX_SUBTRACTIVE(1, 0), |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 190 | &conf->mmio_res_mem); |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void mtk_pcie_domain_set_resources(struct device *dev) |
| 194 | { |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 195 | const mtk_soc_config_t *config = config_of(dev); |
| 196 | const struct mtk_pcie_config *conf = &config->pcie_config; |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 197 | uintptr_t table; |
| 198 | |
| 199 | /* Initialize I/O space constraints. */ |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 200 | table = conf->base + PCIE_TRANS_TABLE_BASE_REG; |
| 201 | if (mtk_pcie_set_trans_window(dev, table, &conf->mmio_res_io) < 0) |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 202 | printk(BIOS_ERR, "%s: Failed to set IO window\n", __func__); |
| 203 | |
| 204 | /* Initialize memory resources constraints. */ |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 205 | table = conf->base + PCIE_TRANS_TABLE_BASE_REG + |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 206 | PCIE_ATR_TLB_SET_OFFSET; |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 207 | if (mtk_pcie_set_trans_window(dev, table, &conf->mmio_res_mem) < 0) |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 208 | printk(BIOS_ERR, "%s: Failed to set MEM window\n", __func__); |
| 209 | |
| 210 | pci_domain_set_resources(dev); |
| 211 | } |
| 212 | |
| 213 | void mtk_pcie_domain_enable(struct device *dev) |
| 214 | { |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 215 | const mtk_soc_config_t *config = config_of(dev); |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 216 | const struct mtk_pcie_config *conf = &config->pcie_config; |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 217 | const char *ltssm_state; |
Jianjun Wang | 79b35ca | 2022-03-30 09:09:43 +0800 | [diff] [blame] | 218 | long perst_time_us; |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 219 | size_t tries = 0; |
| 220 | uint32_t val; |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 221 | |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 222 | /* Set as RC mode */ |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 223 | val = read32p(conf->base + PCIE_SETTING_REG); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 224 | val |= PCIE_RC_MODE; |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 225 | write32p(conf->base + PCIE_SETTING_REG, val); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 226 | |
| 227 | /* Set class code */ |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 228 | val = read32p(conf->base + PCIE_PCI_IDS_1); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 229 | val &= ~GENMASK(31, 8); |
| 230 | val |= PCI_CLASS(PCI_CLASS_BRIDGE_PCI << 8); |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 231 | write32p(conf->base + PCIE_PCI_IDS_1, val); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 232 | |
| 233 | /* Mask all INTx interrupts */ |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 234 | val = read32p(conf->base + PCIE_INT_ENABLE_REG); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 235 | val &= ~PCIE_INTX_ENABLE; |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 236 | write32p(conf->base + PCIE_INT_ENABLE_REG, val); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 237 | |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 238 | perst_time_us = early_init_get_elapsed_time_us(EARLY_INIT_PCIE); |
Jianjun Wang | 79b35ca | 2022-03-30 09:09:43 +0800 | [diff] [blame] | 239 | printk(BIOS_DEBUG, "%s: %ld us elapsed since assert PERST#\n", |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 240 | __func__, perst_time_us); |
| 241 | |
| 242 | /* |
| 243 | * Described in PCIe CEM specification sections 2.2 |
| 244 | * (PERST# Signal) and 2.2.1 (Initial Power-Up (G3 to S0)). |
| 245 | * The deassertion of PERST# should be delayed 100ms (TPVPERL) |
| 246 | * for the power and clock to become stable. |
| 247 | */ |
Jianjun Wang | 79b35ca | 2022-03-30 09:09:43 +0800 | [diff] [blame] | 248 | const long min_perst_time_us = 100000; /* 100 ms */ |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 249 | if (perst_time_us < min_perst_time_us) { |
| 250 | if (!perst_time_us) { |
| 251 | printk(BIOS_WARNING, |
| 252 | "%s: PCIe early init data not found, sleeping 100ms\n", |
| 253 | __func__); |
| 254 | mtk_pcie_reset(conf->base + PCIE_RST_CTRL_REG, true); |
| 255 | } else { |
| 256 | printk(BIOS_WARNING, |
Jianjun Wang | 79b35ca | 2022-03-30 09:09:43 +0800 | [diff] [blame] | 257 | "%s: Need an extra %ld us delay to meet PERST# deassertion requirement\n", |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 258 | __func__, min_perst_time_us - perst_time_us); |
| 259 | } |
| 260 | |
| 261 | udelay(min_perst_time_us - perst_time_us); |
| 262 | } |
| 263 | |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 264 | /* De-assert reset signals */ |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 265 | mtk_pcie_reset(conf->base + PCIE_RST_CTRL_REG, false); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 266 | |
| 267 | if (!retry(100, |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 268 | (tries++, read32p(conf->base + PCIE_LINK_STATUS_REG) & |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 269 | PCIE_CTRL_LINKUP), mdelay(1))) { |
Jianjun Wang | 5111326 | 2022-03-15 13:43:14 +0800 | [diff] [blame] | 270 | val = read32p(conf->base + PCIE_LTSSM_STATUS_REG); |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 271 | ltssm_state = PCIE_LTSSM_STATE(val) >= ARRAY_SIZE(ltssm_str) ? |
| 272 | "Unknown state" : ltssm_str[PCIE_LTSSM_STATE(val)]; |
| 273 | printk(BIOS_ERR, "%s: PCIe link down, current ltssm state: %s\n", |
| 274 | __func__, ltssm_state); |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 275 | return; |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 276 | } |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 277 | |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 278 | printk(BIOS_INFO, "%s: PCIe link up success (%ld tries)\n", __func__, |
| 279 | tries); |
Jianjun Wang | 270b0b6 | 2021-07-14 15:38:19 +0800 | [diff] [blame] | 280 | } |