1 // SPDX-License-Identifier: GPL-2.0+ 2 #include <common.h> 3 #include <dm.h> 4 #include <reset.h> 5 #include <fdtdec.h> 6 #include <pci.h> 7 #include <asm/io.h> 8 #include <asm/arch/h2x_ast2600.h> 9 #include <asm/arch/ahbc_aspeed.h> 10 11 DECLARE_GLOBAL_DATA_PTR; 12 13 /* PCI Host Controller registers */ 14 15 #define ASPEED_PCIE_CLASS_CODE 0x04 16 #define ASPEED_PCIE_GLOBAL 0x30 17 #define ASPEED_PCIE_CFG_DIN 0x50 18 #define ASPEED_PCIE_CFG3 0x58 19 #define ASPEED_PCIE_LOCK 0x7C 20 #define ASPEED_PCIE_LINK 0xC0 21 #define ASPEED_PCIE_INT 0xC4 22 23 /* AST_PCIE_CFG2 0x04 */ 24 #define PCIE_CFG_CLASS_CODE(x) (x << 8) 25 #define PCIE_CFG_REV_ID(x) (x) 26 27 /* AST_PCIE_GLOBAL 0x30 */ 28 #define ROOT_COMPLEX_ID(x) (x << 4) 29 30 /* AST_PCIE_LOCK 0x7C */ 31 #define PCIE_UNLOCK 0xa8 32 33 /* AST_PCIE_LINK 0xC0 */ 34 #define PCIE_LINK_STS BIT(5) 35 36 struct pcie_aspeed { 37 void *ctrl_base; 38 void *h2x_pt; 39 void *cfg_base; 40 fdt_size_t cfg_size; 41 int link_sts; 42 int first_busno; 43 }; 44 45 static int pcie_aspeed_read_config(struct udevice *bus, pci_dev_t bdf, 46 uint offset, ulong *valuep, 47 enum pci_size_t size) 48 { 49 struct pcie_aspeed *pcie = dev_get_priv(bus); 50 51 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) \n", 52 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); 53 54 /* Only allow one other device besides the local one on the local bus */ 55 if (PCI_BUS(bdf) == 1 && PCI_DEV(bdf) != 0) { 56 debug("- out of range\n"); 57 /* 58 * If local dev is 0, the first other dev can 59 * only be 1 60 */ 61 *valuep = pci_get_ff(size); 62 return 0; 63 } 64 65 if (PCI_BUS(bdf) == 1 && (!pcie->link_sts)) { 66 *valuep = pci_get_ff(size); 67 return 0; 68 } 69 70 aspeed_pcie_cfg_read(pcie->h2x_pt, bdf, offset, valuep); 71 72 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, *valuep); 73 *valuep = pci_conv_32_to_size(*valuep, offset, size); 74 75 return 0; 76 } 77 78 static int pcie_aspeed_write_config(struct udevice *bus, pci_dev_t bdf, 79 uint offset, ulong value, 80 enum pci_size_t size) 81 { 82 struct pcie_aspeed *pcie = dev_get_priv(bus); 83 84 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ", 85 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); 86 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value); 87 88 aspeed_pcie_cfg_write(pcie->h2x_pt, bdf, offset, value, size); 89 90 return 0; 91 } 92 93 static int pcie_aspeed_probe(struct udevice *dev) 94 { 95 void *fdt = (void *)gd->fdt_blob; 96 97 struct reset_ctl reset_ctl0, reset_ctl1; 98 struct pcie_aspeed *pcie = dev_get_priv(dev); 99 // struct udevice *ctrl = pci_get_controller(dev); 100 // struct pci_controller *host = dev_get_uclass_priv(ctrl); 101 struct udevice *ahbc_dev; 102 int h2x_of_handle; 103 int ret = 0; 104 105 ret = reset_get_by_index(dev, 0, &reset_ctl0); 106 if (ret) { 107 printf("%s(): Failed to get reset signal\n", __func__); 108 return ret; 109 } 110 111 ret = reset_get_by_index(dev, 0, &reset_ctl1); 112 if (ret) { 113 printf("%s(): Failed to get reset signal\n", __func__); 114 return ret; 115 } 116 117 reset_assert(&reset_ctl0); 118 reset_assert(&reset_ctl1); 119 mdelay(100); 120 reset_deassert(&reset_ctl0); 121 122 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_GET_DRIVER(aspeed_ahbc), 123 &ahbc_dev); 124 if (ret) { 125 debug("ahbc device not defined\n"); 126 return ret; 127 } 128 129 h2x_of_handle = fdtdec_lookup_phandle(fdt, dev_of_offset(dev), "cfg-handle"); 130 if (h2x_of_handle > 0) { 131 pcie->h2x_pt = (void *)fdtdec_get_addr(fdt, h2x_of_handle, "reg"); 132 debug("h2x cfg addr %x \n", (u32)pcie->h2x_pt); 133 } else { 134 debug("h2x device not defined\n"); 135 return h2x_of_handle; 136 } 137 138 aspeed_ahbc_remap_enable(devfdt_get_addr_ptr(ahbc_dev)); 139 140 //plda enable 141 writel(PCIE_UNLOCK, pcie->ctrl_base + ASPEED_PCIE_LOCK); 142 writel(ROOT_COMPLEX_ID(0x3), pcie->ctrl_base + ASPEED_PCIE_GLOBAL); 143 144 pcie->first_busno = dev->seq; 145 mdelay(100); 146 147 /* Don't register host if link is down */ 148 if (readl(pcie->ctrl_base + ASPEED_PCIE_LINK) & PCIE_LINK_STS) { 149 printf("PCIE-%d: Link up\n", dev->seq); 150 pcie->link_sts = 1; 151 } else { 152 printf("PCIE-%d: Link down\n", dev->seq); 153 pcie->link_sts = 0; 154 } 155 aspeed_h2x_rc_enable(pcie->h2x_pt); 156 if(pcie->link_sts) 157 aspeed_pcie_workaround(pcie->h2x_pt); 158 159 return 0; 160 } 161 162 static int pcie_aspeed_ofdata_to_platdata(struct udevice *dev) 163 { 164 struct pcie_aspeed *pcie = dev_get_priv(dev); 165 166 /* Get the controller base address */ 167 pcie->ctrl_base = (void *)devfdt_get_addr_index(dev, 0); 168 169 /* Get the config space base address and size */ 170 pcie->cfg_base = (void *)devfdt_get_addr_size_index(dev, 1, 171 &pcie->cfg_size); 172 173 return 0; 174 } 175 176 static const struct dm_pci_ops pcie_aspeed_ops = { 177 .read_config = pcie_aspeed_read_config, 178 .write_config = pcie_aspeed_write_config, 179 }; 180 181 static const struct udevice_id pcie_aspeed_ids[] = { 182 { .compatible = "aspeed,ast2600-pcie" }, 183 { } 184 }; 185 186 U_BOOT_DRIVER(pcie_aspeed) = { 187 .name = "pcie_aspeed", 188 .id = UCLASS_PCI, 189 .of_match = pcie_aspeed_ids, 190 .ops = &pcie_aspeed_ops, 191 .ofdata_to_platdata = pcie_aspeed_ofdata_to_platdata, 192 .probe = pcie_aspeed_probe, 193 .priv_auto_alloc_size = sizeof(struct pcie_aspeed), 194 }; 195