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-private.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
;
77 /* PCIe Configuration Out/In */
78 static inline void xgene_pcie_cfg_out32(void __iomem
*addr
, int offset
, u32 val
)
80 writel(val
, addr
+ offset
);
83 static inline void xgene_pcie_cfg_out16(void __iomem
*addr
, int offset
, u16 val
)
85 u32 val32
= readl(addr
+ (offset
& ~0x3));
87 switch (offset
& 0x3) {
90 val32
|= (u32
)val
<< 16;
98 writel(val32
, addr
+ (offset
& ~0x3));
101 static inline void xgene_pcie_cfg_out8(void __iomem
*addr
, int offset
, u8 val
)
103 u32 val32
= readl(addr
+ (offset
& ~0x3));
105 switch (offset
& 0x3) {
112 val32
|= (u32
)val
<< 8;
116 val32
|= (u32
)val
<< 16;
120 val32
&= ~0xFF000000;
121 val32
|= (u32
)val
<< 24;
124 writel(val32
, addr
+ (offset
& ~0x3));
127 static inline void xgene_pcie_cfg_in32(void __iomem
*addr
, int offset
, u32
*val
)
129 *val
= readl(addr
+ offset
);
132 static inline void xgene_pcie_cfg_in16(void __iomem
*addr
, int offset
, u32
*val
)
134 *val
= readl(addr
+ (offset
& ~0x3));
136 switch (offset
& 0x3) {
145 static inline void xgene_pcie_cfg_in8(void __iomem
*addr
, int offset
, u32
*val
)
147 *val
= readl(addr
+ (offset
& ~0x3));
149 switch (offset
& 0x3) {
164 * When the address bit [17:16] is 2'b01, the Configuration access will be
165 * treated as Type 1 and it will be forwarded to external PCIe device.
167 static void __iomem
*xgene_pcie_get_cfg_base(struct pci_bus
*bus
)
169 struct xgene_pcie_port
*port
= bus
->sysdata
;
171 if (bus
->number
>= (bus
->primary
+ 1))
172 return port
->cfg_base
+ AXI_EP_CFG_ACCESS
;
174 return port
->cfg_base
;
178 * For Configuration request, RTDID register is used as Bus Number,
179 * Device Number and Function number of the header fields.
181 static void xgene_pcie_set_rtdid_reg(struct pci_bus
*bus
, uint devfn
)
183 struct xgene_pcie_port
*port
= bus
->sysdata
;
184 unsigned int b
, d
, f
;
191 if (!pci_is_root_bus(bus
))
192 rtdid_val
= (b
<< 8) | (d
<< 3) | f
;
194 writel(rtdid_val
, port
->csr_base
+ RTDID
);
195 /* read the register back to ensure flush */
196 readl(port
->csr_base
+ RTDID
);
200 * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
201 * the translation from PCI bus to native BUS. Entire DDR region
202 * is mapped into PCIe space using these registers, so it can be
203 * reached by DMA from EP devices. The BAR0/1 of bridge should be
204 * hidden during enumeration to avoid the sizing and resource allocation
207 static bool xgene_pcie_hide_rc_bars(struct pci_bus
*bus
, int offset
)
209 if (pci_is_root_bus(bus
) && ((offset
== PCI_BASE_ADDRESS_0
) ||
210 (offset
== PCI_BASE_ADDRESS_1
)))
216 static int xgene_pcie_read_config(struct pci_bus
*bus
, unsigned int devfn
,
217 int offset
, int len
, u32
*val
)
219 struct xgene_pcie_port
*port
= bus
->sysdata
;
222 if ((pci_is_root_bus(bus
) && devfn
!= 0) || !port
->link_up
)
223 return PCIBIOS_DEVICE_NOT_FOUND
;
225 if (xgene_pcie_hide_rc_bars(bus
, offset
)) {
227 return PCIBIOS_SUCCESSFUL
;
230 xgene_pcie_set_rtdid_reg(bus
, devfn
);
231 addr
= xgene_pcie_get_cfg_base(bus
);
234 xgene_pcie_cfg_in8(addr
, offset
, val
);
237 xgene_pcie_cfg_in16(addr
, offset
, val
);
240 xgene_pcie_cfg_in32(addr
, offset
, val
);
244 return PCIBIOS_SUCCESSFUL
;
247 static int xgene_pcie_write_config(struct pci_bus
*bus
, unsigned int devfn
,
248 int offset
, int len
, u32 val
)
250 struct xgene_pcie_port
*port
= bus
->sysdata
;
253 if ((pci_is_root_bus(bus
) && devfn
!= 0) || !port
->link_up
)
254 return PCIBIOS_DEVICE_NOT_FOUND
;
256 if (xgene_pcie_hide_rc_bars(bus
, offset
))
257 return PCIBIOS_SUCCESSFUL
;
259 xgene_pcie_set_rtdid_reg(bus
, devfn
);
260 addr
= xgene_pcie_get_cfg_base(bus
);
263 xgene_pcie_cfg_out8(addr
, offset
, (u8
)val
);
266 xgene_pcie_cfg_out16(addr
, offset
, (u16
)val
);
269 xgene_pcie_cfg_out32(addr
, offset
, val
);
273 return PCIBIOS_SUCCESSFUL
;
276 static struct pci_ops xgene_pcie_ops
= {
277 .read
= xgene_pcie_read_config
,
278 .write
= xgene_pcie_write_config
281 static u64
xgene_pcie_set_ib_mask(void __iomem
*csr_base
, u32 addr
,
284 u64 mask
= (~(size
- 1) & PCI_BASE_ADDRESS_MEM_MASK
) | flags
;
288 val32
= readl(csr_base
+ addr
);
289 val
= (val32
& 0x0000ffff) | (lower_32_bits(mask
) << 16);
290 writel(val
, csr_base
+ addr
);
292 val32
= readl(csr_base
+ addr
+ 0x04);
293 val
= (val32
& 0xffff0000) | (lower_32_bits(mask
) >> 16);
294 writel(val
, csr_base
+ addr
+ 0x04);
296 val32
= readl(csr_base
+ addr
+ 0x04);
297 val
= (val32
& 0x0000ffff) | (upper_32_bits(mask
) << 16);
298 writel(val
, csr_base
+ addr
+ 0x04);
300 val32
= readl(csr_base
+ addr
+ 0x08);
301 val
= (val32
& 0xffff0000) | (upper_32_bits(mask
) >> 16);
302 writel(val
, csr_base
+ addr
+ 0x08);
307 static void xgene_pcie_linkup(struct xgene_pcie_port
*port
,
308 u32
*lanes
, u32
*speed
)
310 void __iomem
*csr_base
= port
->csr_base
;
313 port
->link_up
= false;
314 val32
= readl(csr_base
+ PCIECORE_CTLANDSTATUS
);
315 if (val32
& LINK_UP_MASK
) {
316 port
->link_up
= true;
317 *speed
= PIPE_PHY_RATE_RD(val32
);
318 val32
= readl(csr_base
+ BRIDGE_STATUS_0
);
319 *lanes
= val32
>> 26;
323 static int xgene_pcie_init_port(struct xgene_pcie_port
*port
)
327 port
->clk
= clk_get(port
->dev
, NULL
);
328 if (IS_ERR(port
->clk
)) {
329 dev_err(port
->dev
, "clock not available\n");
333 rc
= clk_prepare_enable(port
->clk
);
335 dev_err(port
->dev
, "clock enable failed\n");
342 static int xgene_pcie_map_reg(struct xgene_pcie_port
*port
,
343 struct platform_device
*pdev
)
345 struct resource
*res
;
347 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "csr");
348 port
->csr_base
= devm_ioremap_resource(port
->dev
, res
);
349 if (IS_ERR(port
->csr_base
))
350 return PTR_ERR(port
->csr_base
);
352 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cfg");
353 port
->cfg_base
= devm_ioremap_resource(port
->dev
, res
);
354 if (IS_ERR(port
->cfg_base
))
355 return PTR_ERR(port
->cfg_base
);
356 port
->cfg_addr
= res
->start
;
361 static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port
*port
,
362 struct resource
*res
, u32 offset
,
363 u64 cpu_addr
, u64 pci_addr
)
365 void __iomem
*base
= port
->csr_base
+ offset
;
366 resource_size_t size
= resource_size(res
);
367 u64 restype
= resource_type(res
);
372 if (restype
== IORESOURCE_MEM
) {
379 if (size
>= min_size
)
380 mask
= ~(size
- 1) | flag
;
382 dev_warn(port
->dev
, "res size 0x%llx less than minimum 0x%x\n",
383 (u64
)size
, min_size
);
385 writel(lower_32_bits(cpu_addr
), base
);
386 writel(upper_32_bits(cpu_addr
), base
+ 0x04);
387 writel(lower_32_bits(mask
), base
+ 0x08);
388 writel(upper_32_bits(mask
), base
+ 0x0c);
389 writel(lower_32_bits(pci_addr
), base
+ 0x10);
390 writel(upper_32_bits(pci_addr
), base
+ 0x14);
393 static void xgene_pcie_setup_cfg_reg(void __iomem
*csr_base
, u64 addr
)
395 writel(lower_32_bits(addr
), csr_base
+ CFGBARL
);
396 writel(upper_32_bits(addr
), csr_base
+ CFGBARH
);
397 writel(EN_REG
, csr_base
+ CFGCTL
);
400 static int xgene_pcie_map_ranges(struct xgene_pcie_port
*port
,
401 struct list_head
*res
,
402 resource_size_t io_base
)
404 struct pci_host_bridge_window
*window
;
405 struct device
*dev
= port
->dev
;
408 list_for_each_entry(window
, res
, list
) {
409 struct resource
*res
= window
->res
;
410 u64 restype
= resource_type(res
);
412 dev_dbg(port
->dev
, "%pR\n", res
);
416 xgene_pcie_setup_ob_reg(port
, res
, OMR3BARL
, io_base
,
417 res
->start
- window
->offset
);
418 ret
= pci_remap_iospace(res
, io_base
);
423 xgene_pcie_setup_ob_reg(port
, res
, OMR1BARL
, res
->start
,
424 res
->start
- window
->offset
);
429 dev_err(dev
, "invalid resource %pR\n", res
);
433 xgene_pcie_setup_cfg_reg(port
->csr_base
, port
->cfg_addr
);
438 static void xgene_pcie_setup_pims(void *addr
, u64 pim
, u64 size
)
440 writel(lower_32_bits(pim
), addr
);
441 writel(upper_32_bits(pim
) | EN_COHERENCY
, addr
+ 0x04);
442 writel(lower_32_bits(size
), addr
+ 0x10);
443 writel(upper_32_bits(size
), addr
+ 0x14);
447 * X-Gene PCIe support maximum 3 inbound memory regions
448 * This function helps to select a region based on size of region
450 static int xgene_pcie_select_ib_reg(u8
*ib_reg_mask
, u64 size
)
452 if ((size
> 4) && (size
< SZ_16M
) && !(*ib_reg_mask
& (1 << 1))) {
453 *ib_reg_mask
|= (1 << 1);
457 if ((size
> SZ_1K
) && (size
< SZ_1T
) && !(*ib_reg_mask
& (1 << 0))) {
458 *ib_reg_mask
|= (1 << 0);
462 if ((size
> SZ_1M
) && (size
< SZ_1T
) && !(*ib_reg_mask
& (1 << 2))) {
463 *ib_reg_mask
|= (1 << 2);
470 static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port
*port
,
471 struct of_pci_range
*range
, u8
*ib_reg_mask
)
473 void __iomem
*csr_base
= port
->csr_base
;
474 void __iomem
*cfg_base
= port
->cfg_base
;
477 u64 cpu_addr
= range
->cpu_addr
;
478 u64 pci_addr
= range
->pci_addr
;
479 u64 size
= range
->size
;
480 u64 mask
= ~(size
- 1) | EN_REG
;
481 u32 flags
= PCI_BASE_ADDRESS_MEM_TYPE_64
;
485 region
= xgene_pcie_select_ib_reg(ib_reg_mask
, range
->size
);
487 dev_warn(port
->dev
, "invalid pcie dma-range config\n");
491 if (range
->flags
& IORESOURCE_PREFETCH
)
492 flags
|= PCI_BASE_ADDRESS_MEM_PREFETCH
;
494 bar_low
= pcie_bar_low_val((u32
)cpu_addr
, flags
);
497 xgene_pcie_set_ib_mask(csr_base
, BRIDGE_CFG_4
, flags
, size
);
498 bar_addr
= cfg_base
+ PCI_BASE_ADDRESS_0
;
499 writel(bar_low
, bar_addr
);
500 writel(upper_32_bits(cpu_addr
), bar_addr
+ 0x4);
501 pim_addr
= csr_base
+ PIM1_1L
;
504 bar_addr
= csr_base
+ IBAR2
;
505 writel(bar_low
, bar_addr
);
506 writel(lower_32_bits(mask
), csr_base
+ IR2MSK
);
507 pim_addr
= csr_base
+ PIM2_1L
;
510 bar_addr
= csr_base
+ IBAR3L
;
511 writel(bar_low
, bar_addr
);
512 writel(upper_32_bits(cpu_addr
), bar_addr
+ 0x4);
513 writel(lower_32_bits(mask
), csr_base
+ IR3MSKL
);
514 writel(upper_32_bits(mask
), csr_base
+ IR3MSKL
+ 0x4);
515 pim_addr
= csr_base
+ PIM3_1L
;
519 xgene_pcie_setup_pims(pim_addr
, pci_addr
, ~(size
- 1));
522 static int pci_dma_range_parser_init(struct of_pci_range_parser
*parser
,
523 struct device_node
*node
)
525 const int na
= 3, ns
= 2;
529 parser
->pna
= of_n_addr_cells(node
);
530 parser
->np
= parser
->pna
+ na
+ ns
;
532 parser
->range
= of_get_property(node
, "dma-ranges", &rlen
);
535 parser
->end
= parser
->range
+ rlen
/ sizeof(__be32
);
540 static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port
*port
)
542 struct device_node
*np
= port
->node
;
543 struct of_pci_range range
;
544 struct of_pci_range_parser parser
;
545 struct device
*dev
= port
->dev
;
548 if (pci_dma_range_parser_init(&parser
, np
)) {
549 dev_err(dev
, "missing dma-ranges property\n");
553 /* Get the dma-ranges from DT */
554 for_each_of_pci_range(&parser
, &range
) {
555 u64 end
= range
.cpu_addr
+ range
.size
- 1;
557 dev_dbg(port
->dev
, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
558 range
.flags
, range
.cpu_addr
, end
, range
.pci_addr
);
559 xgene_pcie_setup_ib_reg(port
, &range
, &ib_reg_mask
);
564 /* clear BAR configuration which was done by firmware */
565 static void xgene_pcie_clear_config(struct xgene_pcie_port
*port
)
569 for (i
= PIM1_1L
; i
<= CFGCTL
; i
+= 4)
570 writel(0x0, port
->csr_base
+ i
);
573 static int xgene_pcie_setup(struct xgene_pcie_port
*port
,
574 struct list_head
*res
,
575 resource_size_t io_base
)
577 u32 val
, lanes
= 0, speed
= 0;
580 xgene_pcie_clear_config(port
);
582 /* setup the vendor and device IDs correctly */
583 val
= (XGENE_PCIE_DEVICEID
<< 16) | XGENE_PCIE_VENDORID
;
584 writel(val
, port
->csr_base
+ BRIDGE_CFG_0
);
586 ret
= xgene_pcie_map_ranges(port
, res
, io_base
);
590 ret
= xgene_pcie_parse_map_dma_ranges(port
);
594 xgene_pcie_linkup(port
, &lanes
, &speed
);
596 dev_info(port
->dev
, "(rc) link down\n");
598 dev_info(port
->dev
, "(rc) x%d gen-%d link up\n",
603 static int xgene_pcie_probe_bridge(struct platform_device
*pdev
)
605 struct device_node
*dn
= pdev
->dev
.of_node
;
606 struct xgene_pcie_port
*port
;
607 resource_size_t iobase
= 0;
612 port
= devm_kzalloc(&pdev
->dev
, sizeof(*port
), GFP_KERNEL
);
615 port
->node
= of_node_get(pdev
->dev
.of_node
);
616 port
->dev
= &pdev
->dev
;
618 ret
= xgene_pcie_map_reg(port
, pdev
);
622 ret
= xgene_pcie_init_port(port
);
626 ret
= of_pci_get_host_bridge_resources(dn
, 0, 0xff, &res
, &iobase
);
630 ret
= xgene_pcie_setup(port
, &res
, iobase
);
634 bus
= pci_create_root_bus(&pdev
->dev
, 0,
635 &xgene_pcie_ops
, port
, &res
);
639 pci_scan_child_bus(bus
);
640 pci_assign_unassigned_bus_resources(bus
);
641 pci_bus_add_devices(bus
);
643 platform_set_drvdata(pdev
, port
);
647 static const struct of_device_id xgene_pcie_match_table
[] = {
648 {.compatible
= "apm,xgene-pcie",},
652 static struct platform_driver xgene_pcie_driver
= {
654 .name
= "xgene-pcie",
655 .owner
= THIS_MODULE
,
656 .of_match_table
= of_match_ptr(xgene_pcie_match_table
),
658 .probe
= xgene_pcie_probe_bridge
,
660 module_platform_driver(xgene_pcie_driver
);
662 MODULE_AUTHOR("Tanmay Inamdar <tinamdar@apm.com>");
663 MODULE_DESCRIPTION("APM X-Gene PCIe driver");
664 MODULE_LICENSE("GPL v2");