Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * MediaTek PCIe host controller driver. |
| 4 | * |
| 5 | * Copyright (c) 2017-2019 MediaTek Inc. |
| 6 | * Author: Ryder Lee <ryder.lee@mediatek.com> |
| 7 | * Honghui Zhang <honghui.zhang@mediatek.com> |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <clk.h> |
| 12 | #include <dm.h> |
| 13 | #include <generic-phy.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 16 | #include <pci.h> |
| 17 | #include <reset.h> |
| 18 | #include <asm/io.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <dm/devres.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 21 | #include <linux/iopoll.h> |
| 22 | #include <linux/list.h> |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 23 | #include "pci_internal.h" |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 24 | |
| 25 | /* PCIe shared registers */ |
| 26 | #define PCIE_SYS_CFG 0x00 |
| 27 | #define PCIE_INT_ENABLE 0x0c |
| 28 | #define PCIE_CFG_ADDR 0x20 |
| 29 | #define PCIE_CFG_DATA 0x24 |
| 30 | |
| 31 | /* PCIe per port registers */ |
| 32 | #define PCIE_BAR0_SETUP 0x10 |
| 33 | #define PCIE_CLASS 0x34 |
| 34 | #define PCIE_LINK_STATUS 0x50 |
| 35 | |
| 36 | #define PCIE_PORT_INT_EN(x) BIT(20 + (x)) |
| 37 | #define PCIE_PORT_PERST(x) BIT(1 + (x)) |
| 38 | #define PCIE_PORT_LINKUP BIT(0) |
| 39 | #define PCIE_BAR_MAP_MAX GENMASK(31, 16) |
| 40 | |
| 41 | #define PCIE_BAR_ENABLE BIT(0) |
| 42 | #define PCIE_REVISION_ID BIT(0) |
| 43 | #define PCIE_CLASS_CODE (0x60400 << 8) |
| 44 | #define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \ |
| 45 | ((((regn) >> 8) & GENMASK(3, 0)) << 24)) |
| 46 | #define PCIE_CONF_ADDR(regn, bdf) \ |
| 47 | (PCIE_CONF_REG(regn) | (bdf)) |
| 48 | |
| 49 | /* MediaTek specific configuration registers */ |
| 50 | #define PCIE_FTS_NUM 0x70c |
| 51 | #define PCIE_FTS_NUM_MASK GENMASK(15, 8) |
| 52 | #define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8) |
| 53 | |
| 54 | #define PCIE_FC_CREDIT 0x73c |
| 55 | #define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16)) |
| 56 | #define PCIE_FC_CREDIT_VAL(x) ((x) << 16) |
| 57 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 58 | /* PCIe V2 share registers */ |
| 59 | #define PCIE_SYS_CFG_V2 0x0 |
| 60 | #define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8) |
| 61 | #define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8) |
| 62 | |
| 63 | /* PCIe V2 per-port registers */ |
| 64 | #define PCIE_CONF_VEND_ID 0x100 |
| 65 | #define PCIE_CONF_DEVICE_ID 0x102 |
| 66 | #define PCIE_CONF_CLASS_ID 0x106 |
| 67 | |
| 68 | #define PCIE_AHB_TRANS_BASE0_L 0x438 |
| 69 | #define PCIE_AHB_TRANS_BASE0_H 0x43c |
| 70 | #define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0)) |
| 71 | #define PCIE_AXI_WINDOW0 0x448 |
| 72 | #define WIN_ENABLE BIT(7) |
| 73 | |
| 74 | /* |
| 75 | * Define PCIe to AHB window size as 2^33 to support max 8GB address space |
| 76 | * translate, support least 4GB DRAM size access from EP DMA(physical DRAM |
| 77 | * start from 0x40000000). |
| 78 | */ |
| 79 | #define PCIE2AHB_SIZE 0x21 |
| 80 | |
| 81 | /* PCIe V2 configuration transaction header */ |
| 82 | #define PCIE_CFG_HEADER0 0x460 |
| 83 | #define PCIE_CFG_HEADER1 0x464 |
| 84 | #define PCIE_CFG_HEADER2 0x468 |
| 85 | #define PCIE_CFG_WDATA 0x470 |
| 86 | #define PCIE_APP_TLP_REQ 0x488 |
| 87 | #define PCIE_CFG_RDATA 0x48c |
| 88 | #define APP_CFG_REQ BIT(0) |
| 89 | #define APP_CPL_STATUS GENMASK(7, 5) |
| 90 | |
| 91 | #define CFG_WRRD_TYPE_0 4 |
| 92 | #define CFG_WR_FMT 2 |
| 93 | #define CFG_RD_FMT 0 |
| 94 | |
| 95 | #define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0)) |
| 96 | #define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24)) |
| 97 | #define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29)) |
| 98 | #define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2)) |
| 99 | #define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16)) |
| 100 | #define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19)) |
| 101 | #define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24)) |
| 102 | #define CFG_HEADER_DW0(type, fmt) \ |
| 103 | (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt)) |
| 104 | #define CFG_HEADER_DW1(where, size) \ |
| 105 | (GENMASK(((size) - 1), 0) << ((where) & 0x3)) |
| 106 | #define CFG_HEADER_DW2(regn, fun, dev, bus) \ |
| 107 | (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \ |
| 108 | CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus)) |
| 109 | |
| 110 | #define PCIE_RST_CTRL 0x510 |
| 111 | #define PCIE_PHY_RSTB BIT(0) |
| 112 | #define PCIE_PIPE_SRSTB BIT(1) |
| 113 | #define PCIE_MAC_SRSTB BIT(2) |
| 114 | #define PCIE_CRSTB BIT(3) |
| 115 | #define PCIE_PERSTB BIT(8) |
| 116 | #define PCIE_LINKDOWN_RST_EN GENMASK(15, 13) |
| 117 | #define PCIE_LINK_STATUS_V2 0x804 |
| 118 | #define PCIE_PORT_LINKUP_V2 BIT(11) |
| 119 | |
| 120 | #define PCI_VENDOR_ID_MEDIATEK 0x14c3 |
| 121 | |
| 122 | enum MTK_PCIE_GEN {PCIE_V1, PCIE_V2, PCIE_V3}; |
| 123 | |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 124 | struct mtk_pcie_port { |
| 125 | void __iomem *base; |
| 126 | struct list_head list; |
| 127 | struct mtk_pcie *pcie; |
| 128 | struct reset_ctl reset; |
| 129 | struct clk sys_ck; |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 130 | struct clk ahb_ck; |
| 131 | struct clk axi_ck; |
| 132 | struct clk aux_ck; |
| 133 | struct clk obff_ck; |
| 134 | struct clk pipe_ck; |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 135 | struct phy phy; |
| 136 | u32 slot; |
| 137 | }; |
| 138 | |
| 139 | struct mtk_pcie { |
| 140 | void __iomem *base; |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 141 | void *priv; |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 142 | struct clk free_ck; |
| 143 | struct list_head ports; |
| 144 | }; |
| 145 | |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 146 | static int mtk_pcie_config_address(const struct udevice *udev, pci_dev_t bdf, |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 147 | uint offset, void **paddress) |
| 148 | { |
| 149 | struct mtk_pcie *pcie = dev_get_priv(udev); |
| 150 | |
| 151 | writel(PCIE_CONF_ADDR(offset, bdf), pcie->base + PCIE_CFG_ADDR); |
| 152 | *paddress = pcie->base + PCIE_CFG_DATA + (offset & 3); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Simon Glass | c4e72c4 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 157 | static int mtk_pcie_read_config(const struct udevice *bus, pci_dev_t bdf, |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 158 | uint offset, ulong *valuep, |
| 159 | enum pci_size_t size) |
| 160 | { |
| 161 | return pci_generic_mmap_read_config(bus, mtk_pcie_config_address, |
| 162 | bdf, offset, valuep, size); |
| 163 | } |
| 164 | |
| 165 | static int mtk_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 166 | uint offset, ulong value, |
| 167 | enum pci_size_t size) |
| 168 | { |
| 169 | return pci_generic_mmap_write_config(bus, mtk_pcie_config_address, |
| 170 | bdf, offset, value, size); |
| 171 | } |
| 172 | |
| 173 | static const struct dm_pci_ops mtk_pcie_ops = { |
| 174 | .read_config = mtk_pcie_read_config, |
| 175 | .write_config = mtk_pcie_write_config, |
| 176 | }; |
| 177 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 178 | static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port) |
| 179 | { |
| 180 | u32 val; |
| 181 | int err; |
| 182 | |
| 183 | err = readl_poll_timeout(port->base + PCIE_APP_TLP_REQ, val, |
| 184 | !(val & APP_CFG_REQ), 100 * 1000); |
| 185 | if (err) |
| 186 | return -1; |
| 187 | |
| 188 | if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS) |
| 189 | return -1; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, pci_dev_t devfn, |
| 195 | int where, int size, ulong *val) |
| 196 | { |
| 197 | u32 tmp; |
| 198 | |
| 199 | writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT), |
| 200 | port->base + PCIE_CFG_HEADER0); |
| 201 | writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1); |
| 202 | writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_DEV(devfn), bus), |
| 203 | port->base + PCIE_CFG_HEADER2); |
| 204 | |
| 205 | /* Trigger h/w to transmit Cfgrd TLP */ |
| 206 | tmp = readl(port->base + PCIE_APP_TLP_REQ); |
| 207 | tmp |= APP_CFG_REQ; |
| 208 | writel(tmp, port->base + PCIE_APP_TLP_REQ); |
| 209 | |
| 210 | /* Check completion status */ |
| 211 | if (mtk_pcie_check_cfg_cpld(port)) |
| 212 | return -1; |
| 213 | |
| 214 | /* Read cpld payload of Cfgrd */ |
| 215 | *val = readl(port->base + PCIE_CFG_RDATA); |
| 216 | |
| 217 | if (size == 1) |
| 218 | *val = (*val >> (8 * (where & 3))) & 0xff; |
| 219 | else if (size == 2) |
| 220 | *val = (*val >> (8 * (where & 3))) & 0xffff; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, pci_dev_t devfn, |
| 226 | int where, int size, u32 val) |
| 227 | { |
| 228 | /* Write PCIe configuration transaction header for Cfgwr */ |
| 229 | writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT), |
| 230 | port->base + PCIE_CFG_HEADER0); |
| 231 | writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1); |
| 232 | writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_DEV(devfn), bus), |
| 233 | port->base + PCIE_CFG_HEADER2); |
| 234 | |
| 235 | /* Write Cfgwr data */ |
| 236 | val = val << 8 * (where & 3); |
| 237 | writel(val, port->base + PCIE_CFG_WDATA); |
| 238 | |
| 239 | /* Trigger h/w to transmit Cfgwr TLP */ |
| 240 | val = readl(port->base + PCIE_APP_TLP_REQ); |
| 241 | val |= APP_CFG_REQ; |
| 242 | writel(val, port->base + PCIE_APP_TLP_REQ); |
| 243 | |
| 244 | /* Check completion status */ |
| 245 | return mtk_pcie_check_cfg_cpld(port); |
| 246 | } |
| 247 | |
| 248 | static struct mtk_pcie_port *mtk_pcie_find_port(const struct udevice *bus, |
| 249 | pci_dev_t bdf) |
| 250 | { |
| 251 | struct mtk_pcie *pcie = dev_get_priv(bus); |
| 252 | struct mtk_pcie_port *port; |
| 253 | struct udevice *dev; |
| 254 | struct pci_child_platdata *pplat = NULL; |
| 255 | int ret = 0; |
| 256 | |
| 257 | if (PCI_BUS(bdf) != 0) { |
| 258 | ret = pci_get_bus(PCI_BUS(bdf), &dev); |
| 259 | if (ret) { |
| 260 | debug("No such device,ret = %d\n", ret); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | while (dev->parent->seq != 0) |
| 265 | dev = dev->parent; |
| 266 | |
| 267 | pplat = dev_get_parent_platdata(dev); |
| 268 | } |
| 269 | |
| 270 | list_for_each_entry(port, &pcie->ports, list) { |
| 271 | if ((PCI_BUS(bdf) == 0) && (PCI_DEV(bdf) == port->slot)) |
| 272 | return port; |
| 273 | |
| 274 | if (PCI_BUS(bdf) != 0 && PCI_DEV(bdf) == 0 && |
| 275 | PCI_DEV(pplat->devfn) == port->slot) |
| 276 | return port; |
| 277 | } |
| 278 | |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | static int mtk_pcie_config_read(const struct udevice *bus, pci_dev_t bdf, |
| 283 | uint offset, ulong *valuep, |
| 284 | enum pci_size_t size) |
| 285 | { |
| 286 | struct mtk_pcie_port *port; |
| 287 | int ret; |
| 288 | |
| 289 | port = mtk_pcie_find_port(bus, bdf); |
| 290 | if (!port) { |
| 291 | *valuep = pci_get_ff(size); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | ret = mtk_pcie_hw_rd_cfg(port, PCI_BUS(bdf), bdf, offset, (1 << size), valuep); |
| 296 | if (ret) |
| 297 | *valuep = pci_get_ff(size); |
| 298 | |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static int mtk_pcie_config_write(struct udevice *bus, pci_dev_t bdf, |
| 303 | uint offset, ulong value, |
| 304 | enum pci_size_t size) |
| 305 | { |
| 306 | struct mtk_pcie_port *port; |
| 307 | |
| 308 | port = mtk_pcie_find_port(bus, bdf); |
| 309 | if (!port) |
| 310 | return 0; |
| 311 | |
| 312 | /* Do not modify RC bar 0/1. */ |
| 313 | if (PCI_BUS(bdf) == 0 && (offset == 0x10 || offset == 0x14)) |
| 314 | return 0; |
| 315 | |
| 316 | return mtk_pcie_hw_wr_cfg(port, PCI_BUS(bdf), bdf, offset, (1 << size), value); |
| 317 | } |
| 318 | |
| 319 | static const struct dm_pci_ops mtk_pcie_ops_v2 = { |
| 320 | .read_config = mtk_pcie_config_read, |
| 321 | .write_config = mtk_pcie_config_write, |
| 322 | }; |
| 323 | |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 324 | static void mtk_pcie_port_free(struct mtk_pcie_port *port) |
| 325 | { |
| 326 | list_del(&port->list); |
| 327 | free(port); |
| 328 | } |
| 329 | |
| 330 | static int mtk_pcie_startup_port(struct mtk_pcie_port *port) |
| 331 | { |
| 332 | struct mtk_pcie *pcie = port->pcie; |
| 333 | u32 slot = PCI_DEV(port->slot << 11); |
| 334 | u32 val; |
| 335 | int err; |
| 336 | |
| 337 | /* assert port PERST_N */ |
| 338 | setbits_le32(pcie->base + PCIE_SYS_CFG, PCIE_PORT_PERST(port->slot)); |
| 339 | /* de-assert port PERST_N */ |
| 340 | clrbits_le32(pcie->base + PCIE_SYS_CFG, PCIE_PORT_PERST(port->slot)); |
| 341 | |
| 342 | /* 100ms timeout value should be enough for Gen1/2 training */ |
| 343 | err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val, |
| 344 | !!(val & PCIE_PORT_LINKUP), 100000); |
| 345 | if (err) |
| 346 | return -ETIMEDOUT; |
| 347 | |
| 348 | /* disable interrupt */ |
| 349 | clrbits_le32(pcie->base + PCIE_INT_ENABLE, |
| 350 | PCIE_PORT_INT_EN(port->slot)); |
| 351 | |
| 352 | /* map to all DDR region. We need to set it before cfg operation. */ |
| 353 | writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE, |
| 354 | port->base + PCIE_BAR0_SETUP); |
| 355 | |
| 356 | /* configure class code and revision ID */ |
| 357 | writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS); |
| 358 | |
| 359 | /* configure FC credit */ |
| 360 | writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, slot), |
| 361 | pcie->base + PCIE_CFG_ADDR); |
| 362 | clrsetbits_le32(pcie->base + PCIE_CFG_DATA, PCIE_FC_CREDIT_MASK, |
| 363 | PCIE_FC_CREDIT_VAL(0x806c)); |
| 364 | |
| 365 | /* configure RC FTS number to 250 when it leaves L0s */ |
| 366 | writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, slot), pcie->base + PCIE_CFG_ADDR); |
| 367 | clrsetbits_le32(pcie->base + PCIE_CFG_DATA, PCIE_FTS_NUM_MASK, |
| 368 | PCIE_FTS_NUM_L0(0x50)); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 373 | static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) |
| 374 | { |
| 375 | struct mtk_pcie *pcie = port->pcie; |
| 376 | struct udevice *dev = pcie->priv; |
| 377 | struct pci_region *pci_mem; |
| 378 | u32 val; |
| 379 | int err; |
| 380 | |
| 381 | /* MT7622/MT7629 platforms need to enable LTSSM and ASPM from PCIe subsys */ |
| 382 | if (pcie->base) { |
| 383 | val = readl(pcie->base + PCIE_SYS_CFG_V2); |
| 384 | val |= PCIE_CSR_LTSSM_EN(port->slot) | |
| 385 | PCIE_CSR_ASPM_L1_EN(port->slot); |
| 386 | writel(val, pcie->base + PCIE_SYS_CFG_V2); |
| 387 | } |
| 388 | |
| 389 | /* Assert all reset signals */ |
| 390 | writel(0, port->base + PCIE_RST_CTRL); |
| 391 | |
| 392 | /* |
| 393 | * Enable PCIe link down reset, if link status changed from link up to |
| 394 | * link down, this will reset MAC control registers and configuration |
| 395 | * space. |
| 396 | */ |
| 397 | writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL); |
| 398 | udelay(500); |
| 399 | |
| 400 | /* De-assert PHY, PE, PIPE, MAC and configuration reset */ |
| 401 | val = readl(port->base + PCIE_RST_CTRL); |
| 402 | val |= PCIE_PHY_RSTB | PCIE_PIPE_SRSTB | PCIE_MAC_SRSTB | PCIE_CRSTB; |
| 403 | writel(val, port->base + PCIE_RST_CTRL); |
| 404 | |
| 405 | mdelay(100); |
| 406 | val |= PCIE_PERSTB; |
| 407 | writel(val, port->base + PCIE_RST_CTRL); |
| 408 | |
| 409 | /* Set up vendor ID and class code */ |
| 410 | val = PCI_VENDOR_ID_MEDIATEK; |
| 411 | writew(val, port->base + PCIE_CONF_VEND_ID); |
| 412 | |
| 413 | val = PCI_CLASS_BRIDGE_PCI; |
| 414 | writew(val, port->base + PCIE_CONF_CLASS_ID); |
| 415 | |
| 416 | /* 100ms timeout value should be enough for Gen1/2 training */ |
| 417 | err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val, |
| 418 | !!(val & PCIE_PORT_LINKUP_V2), |
| 419 | 100 * 1000); |
| 420 | if (err) |
| 421 | return -ETIMEDOUT; |
| 422 | |
| 423 | pci_get_regions(dev, NULL, &pci_mem, NULL); |
| 424 | |
| 425 | /* Set AHB to PCIe translation windows */ |
| 426 | val = lower_32_bits(pci_mem->bus_start) | |
| 427 | AHB2PCIE_SIZE(fls(pci_mem->size) - 1); |
| 428 | writel(val, port->base + PCIE_AHB_TRANS_BASE0_L); |
| 429 | |
| 430 | val = upper_32_bits(pci_mem->bus_start); |
| 431 | writel(val, port->base + PCIE_AHB_TRANS_BASE0_H); |
| 432 | |
| 433 | /* Set PCIe to AXI translation memory space.*/ |
| 434 | val = PCIE2AHB_SIZE | WIN_ENABLE; |
| 435 | writel(val, port->base + PCIE_AXI_WINDOW0); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 440 | static void mtk_pcie_enable_port(struct mtk_pcie_port *port) |
| 441 | { |
| 442 | int err; |
| 443 | |
| 444 | err = clk_enable(&port->sys_ck); |
| 445 | if (err) |
| 446 | goto exit; |
| 447 | |
| 448 | err = reset_assert(&port->reset); |
| 449 | if (err) |
| 450 | goto exit; |
| 451 | |
| 452 | err = reset_deassert(&port->reset); |
| 453 | if (err) |
| 454 | goto exit; |
| 455 | |
| 456 | err = generic_phy_init(&port->phy); |
| 457 | if (err) |
| 458 | goto exit; |
| 459 | |
| 460 | err = generic_phy_power_on(&port->phy); |
| 461 | if (err) |
| 462 | goto exit; |
| 463 | |
| 464 | if (!mtk_pcie_startup_port(port)) |
| 465 | return; |
| 466 | |
| 467 | pr_err("Port%d link down\n", port->slot); |
| 468 | exit: |
| 469 | mtk_pcie_port_free(port); |
| 470 | } |
| 471 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 472 | static void mtk_pcie_enable_port_v2(struct mtk_pcie_port *port) |
| 473 | { |
| 474 | int err = 0; |
| 475 | |
| 476 | err = clk_enable(&port->sys_ck); |
| 477 | if (err) { |
| 478 | debug("clk_enable(sys_ck) failed: %d\n", err); |
| 479 | goto exit; |
| 480 | } |
| 481 | |
| 482 | err = clk_enable(&port->ahb_ck); |
| 483 | if (err) { |
| 484 | debug("clk_enable(ahb_ck) failed: %d\n", err); |
| 485 | goto exit; |
| 486 | } |
| 487 | |
| 488 | err = clk_enable(&port->aux_ck); |
| 489 | if (err) { |
| 490 | debug("clk_enable(aux_ck) failed: %d\n", err); |
| 491 | goto exit; |
| 492 | } |
| 493 | |
| 494 | err = clk_enable(&port->axi_ck); |
| 495 | if (err) { |
| 496 | debug("clk_enable(axi_ck) failed: %d\n", err); |
| 497 | goto exit; |
| 498 | } |
| 499 | |
| 500 | err = clk_enable(&port->obff_ck); |
| 501 | if (err) { |
| 502 | debug("clk_enable(obff_ck) failed: %d\n", err); |
| 503 | goto exit; |
| 504 | } |
| 505 | |
| 506 | err = clk_enable(&port->pipe_ck); |
| 507 | if (err) { |
| 508 | debug("clk_enable(pipe_ck) failed: %d\n", err); |
| 509 | goto exit; |
| 510 | } |
| 511 | |
| 512 | err = mtk_pcie_startup_port_v2(port); |
| 513 | if (!err) |
| 514 | return; |
| 515 | |
| 516 | pr_err("Port%d link down\n", port->slot); |
| 517 | exit: |
| 518 | mtk_pcie_port_free(port); |
| 519 | } |
| 520 | |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 521 | static int mtk_pcie_parse_port(struct udevice *dev, u32 slot) |
| 522 | { |
| 523 | struct mtk_pcie *pcie = dev_get_priv(dev); |
| 524 | struct mtk_pcie_port *port; |
| 525 | char name[10]; |
| 526 | int err; |
| 527 | |
| 528 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); |
| 529 | if (!port) |
| 530 | return -ENOMEM; |
| 531 | |
| 532 | snprintf(name, sizeof(name), "port%d", slot); |
| 533 | port->base = dev_remap_addr_name(dev, name); |
| 534 | if (!port->base) |
| 535 | return -ENOENT; |
| 536 | |
| 537 | snprintf(name, sizeof(name), "sys_ck%d", slot); |
| 538 | err = clk_get_by_name(dev, name, &port->sys_ck); |
| 539 | if (err) |
| 540 | return err; |
| 541 | |
| 542 | err = reset_get_by_index(dev, slot, &port->reset); |
| 543 | if (err) |
| 544 | return err; |
| 545 | |
| 546 | err = generic_phy_get_by_index(dev, slot, &port->phy); |
| 547 | if (err) |
| 548 | return err; |
| 549 | |
| 550 | port->slot = slot; |
| 551 | port->pcie = pcie; |
| 552 | |
| 553 | INIT_LIST_HEAD(&port->list); |
| 554 | list_add_tail(&port->list, &pcie->ports); |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 559 | static int mtk_pcie_parse_port_v2(struct udevice *dev, u32 slot) |
| 560 | { |
| 561 | struct mtk_pcie *pcie = dev_get_priv(dev); |
| 562 | struct mtk_pcie_port *port; |
| 563 | char name[10]; |
| 564 | int err; |
| 565 | |
| 566 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); |
| 567 | if (!port) |
| 568 | return -ENOMEM; |
| 569 | |
| 570 | snprintf(name, sizeof(name), "port%d", slot); |
| 571 | port->base = dev_remap_addr_name(dev, name); |
| 572 | if (!port->base) { |
| 573 | debug("failed to map port%d base\n", slot); |
| 574 | return -ENOENT; |
| 575 | } |
| 576 | |
| 577 | snprintf(name, sizeof(name), "sys_ck%d", slot); |
| 578 | err = clk_get_by_name(dev, name, &port->sys_ck); |
| 579 | if (err) { |
| 580 | debug("clk_get_by_name(sys_ck) failed: %d\n", err); |
| 581 | return err; |
| 582 | } |
| 583 | |
| 584 | snprintf(name, sizeof(name), "ahb_ck%d", slot); |
| 585 | err = clk_get_by_name(dev, name, &port->ahb_ck); |
| 586 | if (err) { |
| 587 | debug("clk_get_by_name(ahb_ck) failed: %d\n", err); |
| 588 | return err; |
| 589 | } |
| 590 | |
| 591 | snprintf(name, sizeof(name), "aux_ck%d", slot); |
| 592 | err = clk_get_by_name(dev, name, &port->aux_ck); |
| 593 | if (err) { |
| 594 | debug("clk_get_by_name(aux_ck) failed: %d\n", err); |
| 595 | return err; |
| 596 | } |
| 597 | |
| 598 | snprintf(name, sizeof(name), "axi_ck%d", slot); |
| 599 | err = clk_get_by_name(dev, name, &port->axi_ck); |
| 600 | if (err) { |
| 601 | debug("clk_get_by_name(axi_ck) failed: %d\n", err); |
| 602 | return err; |
| 603 | } |
| 604 | |
| 605 | snprintf(name, sizeof(name), "obff_ck%d", slot); |
| 606 | err = clk_get_by_name(dev, name, &port->obff_ck); |
| 607 | if (err) { |
| 608 | debug("clk_get_by_name(obff_ck) failed: %d\n", err); |
| 609 | return err; |
| 610 | } |
| 611 | |
| 612 | snprintf(name, sizeof(name), "pipe_ck%d", slot); |
| 613 | err = clk_get_by_name(dev, name, &port->pipe_ck); |
| 614 | if (err) { |
| 615 | debug("clk_get_by_name(pipe_ck) failed: %d\n", err); |
| 616 | return err; |
| 617 | } |
| 618 | |
| 619 | port->slot = slot; |
| 620 | port->pcie = pcie; |
| 621 | |
| 622 | INIT_LIST_HEAD(&port->list); |
| 623 | list_add_tail(&port->list, &pcie->ports); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 628 | static int mtk_pcie_probe(struct udevice *dev) |
| 629 | { |
| 630 | struct mtk_pcie *pcie = dev_get_priv(dev); |
| 631 | struct mtk_pcie_port *port, *tmp; |
| 632 | ofnode subnode; |
| 633 | int err; |
| 634 | |
| 635 | INIT_LIST_HEAD(&pcie->ports); |
| 636 | |
| 637 | pcie->base = dev_remap_addr_name(dev, "subsys"); |
| 638 | if (!pcie->base) |
| 639 | return -ENOENT; |
| 640 | |
| 641 | err = clk_get_by_name(dev, "free_ck", &pcie->free_ck); |
| 642 | if (err) |
| 643 | return err; |
| 644 | |
| 645 | /* enable top level clock */ |
| 646 | err = clk_enable(&pcie->free_ck); |
| 647 | if (err) |
| 648 | return err; |
| 649 | |
| 650 | dev_for_each_subnode(subnode, dev) { |
| 651 | struct fdt_pci_addr addr; |
| 652 | u32 slot = 0; |
| 653 | |
| 654 | if (!ofnode_is_available(subnode)) |
| 655 | continue; |
| 656 | |
| 657 | err = ofnode_read_pci_addr(subnode, 0, "reg", &addr); |
| 658 | if (err) |
| 659 | return err; |
| 660 | |
| 661 | slot = PCI_DEV(addr.phys_hi); |
| 662 | |
| 663 | err = mtk_pcie_parse_port(dev, slot); |
| 664 | if (err) |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | /* enable each port, and then check link status */ |
| 669 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) |
| 670 | mtk_pcie_enable_port(port); |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 675 | static int mtk_pcie_probe_v2(struct udevice *dev) |
| 676 | { |
| 677 | struct mtk_pcie *pcie = dev_get_priv(dev); |
| 678 | struct mtk_pcie_port *port, *tmp; |
| 679 | struct fdt_pci_addr addr; |
| 680 | ofnode subnode; |
| 681 | unsigned int slot; |
| 682 | int err; |
| 683 | |
| 684 | INIT_LIST_HEAD(&pcie->ports); |
| 685 | |
| 686 | pcie->base = dev_remap_addr_name(dev, "subsys"); |
| 687 | if (!pcie->base) |
| 688 | return -ENOENT; |
| 689 | |
| 690 | pcie->priv = dev; |
| 691 | |
| 692 | dev_for_each_subnode(subnode, dev) { |
| 693 | if (!ofnode_is_available(subnode)) |
| 694 | continue; |
| 695 | |
| 696 | err = ofnode_read_pci_addr(subnode, 0, "reg", &addr); |
| 697 | if (err) |
| 698 | return err; |
| 699 | |
| 700 | slot = PCI_DEV(addr.phys_hi); |
| 701 | err = mtk_pcie_parse_port_v2(dev, slot); |
| 702 | if (err) |
| 703 | return err; |
| 704 | } |
| 705 | |
| 706 | /* enable each port, and then check link status */ |
| 707 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) |
| 708 | mtk_pcie_enable_port_v2(port); |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 713 | static const struct udevice_id mtk_pcie_ids[] = { |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 714 | { .compatible = "mediatek,mt7623-pcie", PCIE_V1}, |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 715 | { } |
| 716 | }; |
| 717 | |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 718 | U_BOOT_DRIVER(pcie_mediatek_v1) = { |
| 719 | .name = "pcie_mediatek_v1", |
Ryder Lee | 42d3745 | 2019-08-22 12:26:49 +0200 | [diff] [blame] | 720 | .id = UCLASS_PCI, |
| 721 | .of_match = mtk_pcie_ids, |
| 722 | .ops = &mtk_pcie_ops, |
| 723 | .probe = mtk_pcie_probe, |
| 724 | .priv_auto_alloc_size = sizeof(struct mtk_pcie), |
| 725 | }; |
Chuanjia Liu | 91ee45d | 2020-08-10 16:17:10 +0800 | [diff] [blame] | 726 | |
| 727 | static const struct udevice_id mtk_pcie_ids_v2[] = { |
| 728 | { .compatible = "mediatek,mt7622-pcie", PCIE_V2}, |
| 729 | { } |
| 730 | }; |
| 731 | |
| 732 | U_BOOT_DRIVER(pcie_mediatek_v2) = { |
| 733 | .name = "pcie_mediatek_v2", |
| 734 | .id = UCLASS_PCI, |
| 735 | .of_match = mtk_pcie_ids_v2, |
| 736 | .ops = &mtk_pcie_ops_v2, |
| 737 | .probe = mtk_pcie_probe_v2, |
| 738 | .priv_auto_alloc_size = sizeof(struct mtk_pcie), |
| 739 | }; |