2 * APM X-Gene PCIe Driver
4 * Copyright (c) 2014 Applied Micro Circuits Corporation.
6 * Author: Tanmay Inamdar <tinamdar@apm.com>.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/clk.h>
20 #include <linux/delay.h>
22 #include <linux/jiffies.h>
23 #include <linux/memblock.h>
24 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_pci.h>
29 #include <linux/pci.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 #define PCIECORE_CTLANDSTATUS 0x50
41 #define OMR1BARL 0x100
42 #define OMR2BARL 0x118
43 #define OMR3BARL 0x130
48 #define BRIDGE_CFG_0 0x2000
49 #define BRIDGE_CFG_4 0x2010
50 #define BRIDGE_STATUS_0 0x2600
52 #define LINK_UP_MASK 0x00000100
53 #define AXI_EP_CFG_ACCESS 0x10000
54 #define EN_COHERENCY 0xF0000000
55 #define EN_REG 0x00000001
56 #define OB_LO_IO 0x00000002
57 #define XGENE_PCIE_VENDORID 0x10E8
58 #define XGENE_PCIE_DEVICEID 0xE004
59 #define SZ_1T (SZ_1G*1024ULL)
60 #define PIPE_PHY_RATE_RD(src) ((0xc000 & (u32)(src)) >> 0xe)
62 struct xgene_pcie_port
{
63 struct device_node
*node
;
66 void __iomem
*csr_base
;
67 void __iomem
*cfg_base
;
68 unsigned long cfg_addr
;
72 static inline u32
pcie_bar_low_val(u32 addr
, u32 flags
)
74 return (addr
& PCI_BASE_ADDRESS_MEM_MASK
) | flags
;
78 * When the address bit [17:16] is 2'b01, the Configuration access will be
79 * treated as Type 1 and it will be forwarded to external PCIe device.
81 static void __iomem
*xgene_pcie_get_cfg_base(struct pci_bus
*bus
)
83 struct xgene_pcie_port
*port
= bus
->sysdata
;
85 if (bus
->number
>= (bus
->primary
+ 1))
86 return port
->cfg_base
+ AXI_EP_CFG_ACCESS
;
88 return port
->cfg_base
;
92 * For Configuration request, RTDID register is used as Bus Number,
93 * Device Number and Function number of the header fields.
95 static void xgene_pcie_set_rtdid_reg(struct pci_bus
*bus
, uint devfn
)
97 struct xgene_pcie_port
*port
= bus
->sysdata
;
105 if (!pci_is_root_bus(bus
))
106 rtdid_val
= (b
<< 8) | (d
<< 3) | f
;
108 writel(rtdid_val
, port
->csr_base
+ RTDID
);
109 /* read the register back to ensure flush */
110 readl(port
->csr_base
+ RTDID
);
114 * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
115 * the translation from PCI bus to native BUS. Entire DDR region
116 * is mapped into PCIe space using these registers, so it can be
117 * reached by DMA from EP devices. The BAR0/1 of bridge should be
118 * hidden during enumeration to avoid the sizing and resource allocation
121 static bool xgene_pcie_hide_rc_bars(struct pci_bus
*bus
, int offset
)
123 if (pci_is_root_bus(bus
) && ((offset
== PCI_BASE_ADDRESS_0
) ||
124 (offset
== PCI_BASE_ADDRESS_1
)))
130 static void __iomem
*xgene_pcie_map_bus(struct pci_bus
*bus
, unsigned int devfn
,
133 struct xgene_pcie_port
*port
= bus
->sysdata
;
135 if ((pci_is_root_bus(bus
) && devfn
!= 0) || !port
->link_up
||
136 xgene_pcie_hide_rc_bars(bus
, offset
))
139 xgene_pcie_set_rtdid_reg(bus
, devfn
);
140 return xgene_pcie_get_cfg_base(bus
) + offset
;
143 static struct pci_ops xgene_pcie_ops
= {
144 .map_bus
= xgene_pcie_map_bus
,
145 .read
= pci_generic_config_read32
,
146 .write
= pci_generic_config_write32
,
149 static u64
xgene_pcie_set_ib_mask(void __iomem
*csr_base
, u32 addr
,
152 u64 mask
= (~(size
- 1) & PCI_BASE_ADDRESS_MEM_MASK
) | flags
;
156 val32
= readl(csr_base
+ addr
);
157 val
= (val32
& 0x0000ffff) | (lower_32_bits(mask
) << 16);
158 writel(val
, csr_base
+ addr
);
160 val32
= readl(csr_base
+ addr
+ 0x04);
161 val
= (val32
& 0xffff0000) | (lower_32_bits(mask
) >> 16);
162 writel(val
, csr_base
+ addr
+ 0x04);
164 val32
= readl(csr_base
+ addr
+ 0x04);
165 val
= (val32
& 0x0000ffff) | (upper_32_bits(mask
) << 16);
166 writel(val
, csr_base
+ addr
+ 0x04);
168 val32
= readl(csr_base
+ addr
+ 0x08);
169 val
= (val32
& 0xffff0000) | (upper_32_bits(mask
) >> 16);
170 writel(val
, csr_base
+ addr
+ 0x08);
175 static void xgene_pcie_linkup(struct xgene_pcie_port
*port
,
176 u32
*lanes
, u32
*speed
)
178 void __iomem
*csr_base
= port
->csr_base
;
181 port
->link_up
= false;
182 val32
= readl(csr_base
+ PCIECORE_CTLANDSTATUS
);
183 if (val32
& LINK_UP_MASK
) {
184 port
->link_up
= true;
185 *speed
= PIPE_PHY_RATE_RD(val32
);
186 val32
= readl(csr_base
+ BRIDGE_STATUS_0
);
187 *lanes
= val32
>> 26;
191 static int xgene_pcie_init_port(struct xgene_pcie_port
*port
)
195 port
->clk
= clk_get(port
->dev
, NULL
);
196 if (IS_ERR(port
->clk
)) {
197 dev_err(port
->dev
, "clock not available\n");
201 rc
= clk_prepare_enable(port
->clk
);
203 dev_err(port
->dev
, "clock enable failed\n");
210 static int xgene_pcie_map_reg(struct xgene_pcie_port
*port
,
211 struct platform_device
*pdev
)
213 struct resource
*res
;
215 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "csr");
216 port
->csr_base
= devm_ioremap_resource(port
->dev
, res
);
217 if (IS_ERR(port
->csr_base
))
218 return PTR_ERR(port
->csr_base
);
220 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cfg");
221 port
->cfg_base
= devm_ioremap_resource(port
->dev
, res
);
222 if (IS_ERR(port
->cfg_base
))
223 return PTR_ERR(port
->cfg_base
);
224 port
->cfg_addr
= res
->start
;
229 static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port
*port
,
230 struct resource
*res
, u32 offset
,
231 u64 cpu_addr
, u64 pci_addr
)
233 void __iomem
*base
= port
->csr_base
+ offset
;
234 resource_size_t size
= resource_size(res
);
235 u64 restype
= resource_type(res
);
240 if (restype
== IORESOURCE_MEM
) {
247 if (size
>= min_size
)
248 mask
= ~(size
- 1) | flag
;
250 dev_warn(port
->dev
, "res size 0x%llx less than minimum 0x%x\n",
251 (u64
)size
, min_size
);
253 writel(lower_32_bits(cpu_addr
), base
);
254 writel(upper_32_bits(cpu_addr
), base
+ 0x04);
255 writel(lower_32_bits(mask
), base
+ 0x08);
256 writel(upper_32_bits(mask
), base
+ 0x0c);
257 writel(lower_32_bits(pci_addr
), base
+ 0x10);
258 writel(upper_32_bits(pci_addr
), base
+ 0x14);
261 static void xgene_pcie_setup_cfg_reg(void __iomem
*csr_base
, u64 addr
)
263 writel(lower_32_bits(addr
), csr_base
+ CFGBARL
);
264 writel(upper_32_bits(addr
), csr_base
+ CFGBARH
);
265 writel(EN_REG
, csr_base
+ CFGCTL
);
268 static int xgene_pcie_map_ranges(struct xgene_pcie_port
*port
,
269 struct list_head
*res
,
270 resource_size_t io_base
)
272 struct resource_entry
*window
;
273 struct device
*dev
= port
->dev
;
276 resource_list_for_each_entry(window
, res
) {
277 struct resource
*res
= window
->res
;
278 u64 restype
= resource_type(res
);
280 dev_dbg(port
->dev
, "%pR\n", res
);
284 xgene_pcie_setup_ob_reg(port
, res
, OMR3BARL
, io_base
,
285 res
->start
- window
->offset
);
286 ret
= pci_remap_iospace(res
, io_base
);
291 xgene_pcie_setup_ob_reg(port
, res
, OMR1BARL
, res
->start
,
292 res
->start
- window
->offset
);
297 dev_err(dev
, "invalid resource %pR\n", res
);
301 xgene_pcie_setup_cfg_reg(port
->csr_base
, port
->cfg_addr
);
306 static void xgene_pcie_setup_pims(void *addr
, u64 pim
, u64 size
)
308 writel(lower_32_bits(pim
), addr
);
309 writel(upper_32_bits(pim
) | EN_COHERENCY
, addr
+ 0x04);
310 writel(lower_32_bits(size
), addr
+ 0x10);
311 writel(upper_32_bits(size
), addr
+ 0x14);
315 * X-Gene PCIe support maximum 3 inbound memory regions
316 * This function helps to select a region based on size of region
318 static int xgene_pcie_select_ib_reg(u8
*ib_reg_mask
, u64 size
)
320 if ((size
> 4) && (size
< SZ_16M
) && !(*ib_reg_mask
& (1 << 1))) {
321 *ib_reg_mask
|= (1 << 1);
325 if ((size
> SZ_1K
) && (size
< SZ_1T
) && !(*ib_reg_mask
& (1 << 0))) {
326 *ib_reg_mask
|= (1 << 0);
330 if ((size
> SZ_1M
) && (size
< SZ_1T
) && !(*ib_reg_mask
& (1 << 2))) {
331 *ib_reg_mask
|= (1 << 2);
338 static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port
*port
,
339 struct of_pci_range
*range
, u8
*ib_reg_mask
)
341 void __iomem
*csr_base
= port
->csr_base
;
342 void __iomem
*cfg_base
= port
->cfg_base
;
345 u64 cpu_addr
= range
->cpu_addr
;
346 u64 pci_addr
= range
->pci_addr
;
347 u64 size
= range
->size
;
348 u64 mask
= ~(size
- 1) | EN_REG
;
349 u32 flags
= PCI_BASE_ADDRESS_MEM_TYPE_64
;
353 region
= xgene_pcie_select_ib_reg(ib_reg_mask
, range
->size
);
355 dev_warn(port
->dev
, "invalid pcie dma-range config\n");
359 if (range
->flags
& IORESOURCE_PREFETCH
)
360 flags
|= PCI_BASE_ADDRESS_MEM_PREFETCH
;
362 bar_low
= pcie_bar_low_val((u32
)cpu_addr
, flags
);
365 xgene_pcie_set_ib_mask(csr_base
, BRIDGE_CFG_4
, flags
, size
);
366 bar_addr
= cfg_base
+ PCI_BASE_ADDRESS_0
;
367 writel(bar_low
, bar_addr
);
368 writel(upper_32_bits(cpu_addr
), bar_addr
+ 0x4);
369 pim_addr
= csr_base
+ PIM1_1L
;
372 bar_addr
= csr_base
+ IBAR2
;
373 writel(bar_low
, bar_addr
);
374 writel(lower_32_bits(mask
), csr_base
+ IR2MSK
);
375 pim_addr
= csr_base
+ PIM2_1L
;
378 bar_addr
= csr_base
+ IBAR3L
;
379 writel(bar_low
, bar_addr
);
380 writel(upper_32_bits(cpu_addr
), bar_addr
+ 0x4);
381 writel(lower_32_bits(mask
), csr_base
+ IR3MSKL
);
382 writel(upper_32_bits(mask
), csr_base
+ IR3MSKL
+ 0x4);
383 pim_addr
= csr_base
+ PIM3_1L
;
387 xgene_pcie_setup_pims(pim_addr
, pci_addr
, ~(size
- 1));
390 static int pci_dma_range_parser_init(struct of_pci_range_parser
*parser
,
391 struct device_node
*node
)
393 const int na
= 3, ns
= 2;
397 parser
->pna
= of_n_addr_cells(node
);
398 parser
->np
= parser
->pna
+ na
+ ns
;
400 parser
->range
= of_get_property(node
, "dma-ranges", &rlen
);
403 parser
->end
= parser
->range
+ rlen
/ sizeof(__be32
);
408 static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port
*port
)
410 struct device_node
*np
= port
->node
;
411 struct of_pci_range range
;
412 struct of_pci_range_parser parser
;
413 struct device
*dev
= port
->dev
;
416 if (pci_dma_range_parser_init(&parser
, np
)) {
417 dev_err(dev
, "missing dma-ranges property\n");
421 /* Get the dma-ranges from DT */
422 for_each_of_pci_range(&parser
, &range
) {
423 u64 end
= range
.cpu_addr
+ range
.size
- 1;
425 dev_dbg(port
->dev
, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
426 range
.flags
, range
.cpu_addr
, end
, range
.pci_addr
);
427 xgene_pcie_setup_ib_reg(port
, &range
, &ib_reg_mask
);
432 /* clear BAR configuration which was done by firmware */
433 static void xgene_pcie_clear_config(struct xgene_pcie_port
*port
)
437 for (i
= PIM1_1L
; i
<= CFGCTL
; i
+= 4)
438 writel(0x0, port
->csr_base
+ i
);
441 static int xgene_pcie_setup(struct xgene_pcie_port
*port
,
442 struct list_head
*res
,
443 resource_size_t io_base
)
445 u32 val
, lanes
= 0, speed
= 0;
448 xgene_pcie_clear_config(port
);
450 /* setup the vendor and device IDs correctly */
451 val
= (XGENE_PCIE_DEVICEID
<< 16) | XGENE_PCIE_VENDORID
;
452 writel(val
, port
->csr_base
+ BRIDGE_CFG_0
);
454 ret
= xgene_pcie_map_ranges(port
, res
, io_base
);
458 ret
= xgene_pcie_parse_map_dma_ranges(port
);
462 xgene_pcie_linkup(port
, &lanes
, &speed
);
464 dev_info(port
->dev
, "(rc) link down\n");
466 dev_info(port
->dev
, "(rc) x%d gen-%d link up\n",
471 static int xgene_pcie_probe_bridge(struct platform_device
*pdev
)
473 struct device_node
*dn
= pdev
->dev
.of_node
;
474 struct xgene_pcie_port
*port
;
475 resource_size_t iobase
= 0;
480 port
= devm_kzalloc(&pdev
->dev
, sizeof(*port
), GFP_KERNEL
);
483 port
->node
= of_node_get(pdev
->dev
.of_node
);
484 port
->dev
= &pdev
->dev
;
486 ret
= xgene_pcie_map_reg(port
, pdev
);
490 ret
= xgene_pcie_init_port(port
);
494 ret
= of_pci_get_host_bridge_resources(dn
, 0, 0xff, &res
, &iobase
);
498 ret
= xgene_pcie_setup(port
, &res
, iobase
);
502 bus
= pci_create_root_bus(&pdev
->dev
, 0,
503 &xgene_pcie_ops
, port
, &res
);
507 pci_scan_child_bus(bus
);
508 pci_assign_unassigned_bus_resources(bus
);
509 pci_bus_add_devices(bus
);
511 platform_set_drvdata(pdev
, port
);
515 static const struct of_device_id xgene_pcie_match_table
[] = {
516 {.compatible
= "apm,xgene-pcie",},
520 static struct platform_driver xgene_pcie_driver
= {
522 .name
= "xgene-pcie",
523 .of_match_table
= of_match_ptr(xgene_pcie_match_table
),
525 .probe
= xgene_pcie_probe_bridge
,
527 module_platform_driver(xgene_pcie_driver
);
529 MODULE_AUTHOR("Tanmay Inamdar <tinamdar@apm.com>");
530 MODULE_DESCRIPTION("APM X-Gene PCIe driver");
531 MODULE_LICENSE("GPL v2");