1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Amazon's Annapurna Labs IP (used in chips
4 * such as Graviton and Alpine)
6 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
8 * Author: Jonathan Chocron <jonnyc@amazon.com>
11 #include <linux/pci.h>
12 #include <linux/pci-ecam.h>
13 #include <linux/pci-acpi.h>
14 #include "../../pci.h"
16 #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
19 void __iomem
*dbi_base
;
22 static void __iomem
*al_pcie_map_bus(struct pci_bus
*bus
, unsigned int devfn
,
25 struct pci_config_window
*cfg
= bus
->sysdata
;
26 struct al_pcie_acpi
*pcie
= cfg
->priv
;
27 void __iomem
*dbi_base
= pcie
->dbi_base
;
29 if (bus
->number
== cfg
->busr
.start
) {
31 * The DW PCIe core doesn't filter out transactions to other
32 * devices/functions on the root bus num, so we do this here.
34 if (PCI_SLOT(devfn
) > 0)
37 return dbi_base
+ where
;
40 return pci_ecam_map_bus(bus
, devfn
, where
);
43 static int al_pcie_init(struct pci_config_window
*cfg
)
45 struct device
*dev
= cfg
->parent
;
46 struct acpi_device
*adev
= to_acpi_device(dev
);
47 struct acpi_pci_root
*root
= acpi_driver_data(adev
);
48 struct al_pcie_acpi
*al_pcie
;
52 al_pcie
= devm_kzalloc(dev
, sizeof(*al_pcie
), GFP_KERNEL
);
56 res
= devm_kzalloc(dev
, sizeof(*res
), GFP_KERNEL
);
60 ret
= acpi_get_rc_resources(dev
, "AMZN0001", root
->segment
, res
);
62 dev_err(dev
, "can't get rc dbi base address for SEG %d\n",
67 dev_dbg(dev
, "Root port dbi res: %pR\n", res
);
69 al_pcie
->dbi_base
= devm_pci_remap_cfg_resource(dev
, res
);
70 if (IS_ERR(al_pcie
->dbi_base
)) {
71 long err
= PTR_ERR(al_pcie
->dbi_base
);
73 dev_err(dev
, "couldn't remap dbi base %pR (err:%ld)\n",
83 struct pci_ecam_ops al_pcie_ops
= {
87 .map_bus
= al_pcie_map_bus
,
88 .read
= pci_generic_config_read
,
89 .write
= pci_generic_config_write
,
93 #endif /* defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS) */
97 #include <linux/of_pci.h>
98 #include "pcie-designware.h"
100 #define AL_PCIE_REV_ID_2 2
101 #define AL_PCIE_REV_ID_3 3
102 #define AL_PCIE_REV_ID_4 4
104 #define AXI_BASE_OFFSET 0x0
106 #define DEVICE_ID_OFFSET 0x16c
108 #define DEVICE_REV_ID 0x0
109 #define DEVICE_REV_ID_DEV_ID_MASK GENMASK(31, 16)
111 #define DEVICE_REV_ID_DEV_ID_X4 0
112 #define DEVICE_REV_ID_DEV_ID_X8 2
113 #define DEVICE_REV_ID_DEV_ID_X16 4
115 #define OB_CTRL_REV1_2_OFFSET 0x0040
116 #define OB_CTRL_REV3_5_OFFSET 0x0030
118 #define CFG_TARGET_BUS 0x0
119 #define CFG_TARGET_BUS_MASK_MASK GENMASK(7, 0)
120 #define CFG_TARGET_BUS_BUSNUM_MASK GENMASK(15, 8)
122 #define CFG_CONTROL 0x4
123 #define CFG_CONTROL_SUBBUS_MASK GENMASK(15, 8)
124 #define CFG_CONTROL_SEC_BUS_MASK GENMASK(23, 16)
126 struct al_pcie_reg_offsets
{
127 unsigned int ob_ctrl
;
130 struct al_pcie_target_bus_cfg
{
138 void __iomem
*controller_base
; /* base of PCIe unit (not DW core) */
140 resource_size_t ecam_size
;
141 unsigned int controller_rev_id
;
142 struct al_pcie_reg_offsets reg_offsets
;
143 struct al_pcie_target_bus_cfg target_bus_cfg
;
146 #define PCIE_ECAM_DEVFN(x) (((x) & 0xff) << 12)
148 #define to_al_pcie(x) dev_get_drvdata((x)->dev)
150 static inline u32
al_pcie_controller_readl(struct al_pcie
*pcie
, u32 offset
)
152 return readl_relaxed(pcie
->controller_base
+ offset
);
155 static inline void al_pcie_controller_writel(struct al_pcie
*pcie
, u32 offset
,
158 writel_relaxed(val
, pcie
->controller_base
+ offset
);
161 static int al_pcie_rev_id_get(struct al_pcie
*pcie
, unsigned int *rev_id
)
166 dev_rev_id_val
= al_pcie_controller_readl(pcie
, AXI_BASE_OFFSET
+
169 dev_id_val
= FIELD_GET(DEVICE_REV_ID_DEV_ID_MASK
, dev_rev_id_val
);
171 switch (dev_id_val
) {
172 case DEVICE_REV_ID_DEV_ID_X4
:
173 *rev_id
= AL_PCIE_REV_ID_2
;
175 case DEVICE_REV_ID_DEV_ID_X8
:
176 *rev_id
= AL_PCIE_REV_ID_3
;
178 case DEVICE_REV_ID_DEV_ID_X16
:
179 *rev_id
= AL_PCIE_REV_ID_4
;
182 dev_err(pcie
->dev
, "Unsupported dev_id_val (0x%x)\n",
187 dev_dbg(pcie
->dev
, "dev_id_val: 0x%x\n", dev_id_val
);
192 static int al_pcie_reg_offsets_set(struct al_pcie
*pcie
)
194 switch (pcie
->controller_rev_id
) {
195 case AL_PCIE_REV_ID_2
:
196 pcie
->reg_offsets
.ob_ctrl
= OB_CTRL_REV1_2_OFFSET
;
198 case AL_PCIE_REV_ID_3
:
199 case AL_PCIE_REV_ID_4
:
200 pcie
->reg_offsets
.ob_ctrl
= OB_CTRL_REV3_5_OFFSET
;
203 dev_err(pcie
->dev
, "Unsupported controller rev_id: 0x%x\n",
204 pcie
->controller_rev_id
);
211 static inline void al_pcie_target_bus_set(struct al_pcie
*pcie
,
217 reg
= FIELD_PREP(CFG_TARGET_BUS_MASK_MASK
, mask_target_bus
) |
218 FIELD_PREP(CFG_TARGET_BUS_BUSNUM_MASK
, target_bus
);
220 al_pcie_controller_writel(pcie
, AXI_BASE_OFFSET
+
221 pcie
->reg_offsets
.ob_ctrl
+ CFG_TARGET_BUS
,
225 static void __iomem
*al_pcie_conf_addr_map(struct al_pcie
*pcie
,
229 struct al_pcie_target_bus_cfg
*target_bus_cfg
= &pcie
->target_bus_cfg
;
230 unsigned int busnr_ecam
= busnr
& target_bus_cfg
->ecam_mask
;
231 unsigned int busnr_reg
= busnr
& target_bus_cfg
->reg_mask
;
232 struct pcie_port
*pp
= &pcie
->pci
->pp
;
233 void __iomem
*pci_base_addr
;
235 pci_base_addr
= (void __iomem
*)((uintptr_t)pp
->va_cfg0_base
+
237 PCIE_ECAM_DEVFN(devfn
));
239 if (busnr_reg
!= target_bus_cfg
->reg_val
) {
240 dev_dbg(pcie
->pci
->dev
, "Changing target bus busnum val from 0x%x to 0x%x\n",
241 target_bus_cfg
->reg_val
, busnr_reg
);
242 target_bus_cfg
->reg_val
= busnr_reg
;
243 al_pcie_target_bus_set(pcie
,
244 target_bus_cfg
->reg_val
,
245 target_bus_cfg
->reg_mask
);
248 return pci_base_addr
;
251 static int al_pcie_rd_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
252 unsigned int devfn
, int where
, int size
,
255 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
256 struct al_pcie
*pcie
= to_al_pcie(pci
);
257 unsigned int busnr
= bus
->number
;
258 void __iomem
*pci_addr
;
261 pci_addr
= al_pcie_conf_addr_map(pcie
, busnr
, devfn
);
263 rc
= dw_pcie_read(pci_addr
+ where
, size
, val
);
265 dev_dbg(pci
->dev
, "%d-byte config read from %04x:%02x:%02x.%d offset 0x%x (pci_addr: 0x%px) - val:0x%x\n",
266 size
, pci_domain_nr(bus
), bus
->number
,
267 PCI_SLOT(devfn
), PCI_FUNC(devfn
), where
,
268 (pci_addr
+ where
), *val
);
273 static int al_pcie_wr_other_conf(struct pcie_port
*pp
, struct pci_bus
*bus
,
274 unsigned int devfn
, int where
, int size
,
277 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
278 struct al_pcie
*pcie
= to_al_pcie(pci
);
279 unsigned int busnr
= bus
->number
;
280 void __iomem
*pci_addr
;
283 pci_addr
= al_pcie_conf_addr_map(pcie
, busnr
, devfn
);
285 rc
= dw_pcie_write(pci_addr
+ where
, size
, val
);
287 dev_dbg(pci
->dev
, "%d-byte config write to %04x:%02x:%02x.%d offset 0x%x (pci_addr: 0x%px) - val:0x%x\n",
288 size
, pci_domain_nr(bus
), bus
->number
,
289 PCI_SLOT(devfn
), PCI_FUNC(devfn
), where
,
290 (pci_addr
+ where
), val
);
295 static void al_pcie_config_prepare(struct al_pcie
*pcie
)
297 struct al_pcie_target_bus_cfg
*target_bus_cfg
;
298 struct pcie_port
*pp
= &pcie
->pci
->pp
;
299 unsigned int ecam_bus_mask
;
300 u32 cfg_control_offset
;
306 target_bus_cfg
= &pcie
->target_bus_cfg
;
308 ecam_bus_mask
= (pcie
->ecam_size
>> 20) - 1;
309 if (ecam_bus_mask
> 255) {
310 dev_warn(pcie
->dev
, "ECAM window size is larger than 256MB. Cutting off at 256\n");
314 /* This portion is taken from the transaction address */
315 target_bus_cfg
->ecam_mask
= ecam_bus_mask
;
316 /* This portion is taken from the cfg_target_bus reg */
317 target_bus_cfg
->reg_mask
= ~target_bus_cfg
->ecam_mask
;
318 target_bus_cfg
->reg_val
= pp
->busn
->start
& target_bus_cfg
->reg_mask
;
320 al_pcie_target_bus_set(pcie
, target_bus_cfg
->reg_val
,
321 target_bus_cfg
->reg_mask
);
323 secondary_bus
= pp
->busn
->start
+ 1;
324 subordinate_bus
= pp
->busn
->end
;
326 /* Set the valid values of secondary and subordinate buses */
327 cfg_control_offset
= AXI_BASE_OFFSET
+ pcie
->reg_offsets
.ob_ctrl
+
330 cfg_control
= al_pcie_controller_readl(pcie
, cfg_control_offset
);
333 ~(CFG_CONTROL_SEC_BUS_MASK
| CFG_CONTROL_SUBBUS_MASK
);
335 reg
|= FIELD_PREP(CFG_CONTROL_SUBBUS_MASK
, subordinate_bus
) |
336 FIELD_PREP(CFG_CONTROL_SEC_BUS_MASK
, secondary_bus
);
338 al_pcie_controller_writel(pcie
, cfg_control_offset
, reg
);
341 static int al_pcie_host_init(struct pcie_port
*pp
)
343 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
344 struct al_pcie
*pcie
= to_al_pcie(pci
);
347 rc
= al_pcie_rev_id_get(pcie
, &pcie
->controller_rev_id
);
351 rc
= al_pcie_reg_offsets_set(pcie
);
355 al_pcie_config_prepare(pcie
);
360 static const struct dw_pcie_host_ops al_pcie_host_ops
= {
361 .rd_other_conf
= al_pcie_rd_other_conf
,
362 .wr_other_conf
= al_pcie_wr_other_conf
,
363 .host_init
= al_pcie_host_init
,
366 static int al_add_pcie_port(struct pcie_port
*pp
,
367 struct platform_device
*pdev
)
369 struct device
*dev
= &pdev
->dev
;
372 pp
->ops
= &al_pcie_host_ops
;
374 ret
= dw_pcie_host_init(pp
);
376 dev_err(dev
, "failed to initialize host\n");
383 static const struct dw_pcie_ops dw_pcie_ops
= {
386 static int al_pcie_probe(struct platform_device
*pdev
)
388 struct device
*dev
= &pdev
->dev
;
389 struct resource
*controller_res
;
390 struct resource
*ecam_res
;
391 struct resource
*dbi_res
;
392 struct al_pcie
*al_pcie
;
395 al_pcie
= devm_kzalloc(dev
, sizeof(*al_pcie
), GFP_KERNEL
);
399 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
404 pci
->ops
= &dw_pcie_ops
;
409 dbi_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbi");
410 pci
->dbi_base
= devm_pci_remap_cfg_resource(dev
, dbi_res
);
411 if (IS_ERR(pci
->dbi_base
)) {
412 dev_err(dev
, "couldn't remap dbi base %pR\n", dbi_res
);
413 return PTR_ERR(pci
->dbi_base
);
416 ecam_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "config");
418 dev_err(dev
, "couldn't find 'config' reg in DT\n");
421 al_pcie
->ecam_size
= resource_size(ecam_res
);
423 controller_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
425 al_pcie
->controller_base
= devm_ioremap_resource(dev
, controller_res
);
426 if (IS_ERR(al_pcie
->controller_base
)) {
427 dev_err(dev
, "couldn't remap controller base %pR\n",
429 return PTR_ERR(al_pcie
->controller_base
);
432 dev_dbg(dev
, "From DT: dbi_base: %pR, controller_base: %pR\n",
433 dbi_res
, controller_res
);
435 platform_set_drvdata(pdev
, al_pcie
);
437 return al_add_pcie_port(&pci
->pp
, pdev
);
440 static const struct of_device_id al_pcie_of_match
[] = {
441 { .compatible
= "amazon,al-alpine-v2-pcie",
443 { .compatible
= "amazon,al-alpine-v3-pcie",
448 static struct platform_driver al_pcie_driver
= {
451 .of_match_table
= al_pcie_of_match
,
452 .suppress_bind_attrs
= true,
454 .probe
= al_pcie_probe
,
456 builtin_platform_driver(al_pcie_driver
);
458 #endif /* CONFIG_PCIE_AL*/