162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * PCIe host controller driver for Mobiveil PCIe Host controller
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2018 Mobiveil Inc.
662306a36Sopenharmony_ci * Copyright 2019 NXP
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Author: Subrahmanya Lingappa <l.subrahmanya@mobiveil.co.in>
962306a36Sopenharmony_ci *	   Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifndef _PCIE_MOBIVEIL_H
1362306a36Sopenharmony_ci#define _PCIE_MOBIVEIL_H
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/pci.h>
1662306a36Sopenharmony_ci#include <linux/irq.h>
1762306a36Sopenharmony_ci#include <linux/msi.h>
1862306a36Sopenharmony_ci#include "../../pci.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* register offsets and bit positions */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/*
2362306a36Sopenharmony_ci * translation tables are grouped into windows, each window registers are
2462306a36Sopenharmony_ci * grouped into blocks of 4 or 16 registers each
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci#define PAB_REG_BLOCK_SIZE		16
2762306a36Sopenharmony_ci#define PAB_EXT_REG_BLOCK_SIZE		4
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define PAB_REG_ADDR(offset, win)	\
3062306a36Sopenharmony_ci	(offset + (win * PAB_REG_BLOCK_SIZE))
3162306a36Sopenharmony_ci#define PAB_EXT_REG_ADDR(offset, win)	\
3262306a36Sopenharmony_ci	(offset + (win * PAB_EXT_REG_BLOCK_SIZE))
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define LTSSM_STATUS			0x0404
3562306a36Sopenharmony_ci#define  LTSSM_STATUS_L0_MASK		0x3f
3662306a36Sopenharmony_ci#define  LTSSM_STATUS_L0		0x2d
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define PAB_CTRL			0x0808
3962306a36Sopenharmony_ci#define  AMBA_PIO_ENABLE_SHIFT		0
4062306a36Sopenharmony_ci#define  PEX_PIO_ENABLE_SHIFT		1
4162306a36Sopenharmony_ci#define  PAGE_SEL_SHIFT			13
4262306a36Sopenharmony_ci#define  PAGE_SEL_MASK			0x3f
4362306a36Sopenharmony_ci#define  PAGE_LO_MASK			0x3ff
4462306a36Sopenharmony_ci#define  PAGE_SEL_OFFSET_SHIFT		10
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define PAB_ACTIVITY_STAT		0x81c
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#define PAB_AXI_PIO_CTRL		0x0840
4962306a36Sopenharmony_ci#define  APIO_EN_MASK			0xf
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define PAB_PEX_PIO_CTRL		0x08c0
5262306a36Sopenharmony_ci#define  PIO_ENABLE_SHIFT		0
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define PAB_INTP_AMBA_MISC_ENB		0x0b0c
5562306a36Sopenharmony_ci#define PAB_INTP_AMBA_MISC_STAT		0x0b1c
5662306a36Sopenharmony_ci#define  PAB_INTP_RESET			BIT(1)
5762306a36Sopenharmony_ci#define  PAB_INTP_MSI			BIT(3)
5862306a36Sopenharmony_ci#define  PAB_INTP_INTA			BIT(5)
5962306a36Sopenharmony_ci#define  PAB_INTP_INTB			BIT(6)
6062306a36Sopenharmony_ci#define  PAB_INTP_INTC			BIT(7)
6162306a36Sopenharmony_ci#define  PAB_INTP_INTD			BIT(8)
6262306a36Sopenharmony_ci#define  PAB_INTP_PCIE_UE		BIT(9)
6362306a36Sopenharmony_ci#define  PAB_INTP_IE_PMREDI		BIT(29)
6462306a36Sopenharmony_ci#define  PAB_INTP_IE_EC			BIT(30)
6562306a36Sopenharmony_ci#define  PAB_INTP_MSI_MASK		PAB_INTP_MSI
6662306a36Sopenharmony_ci#define  PAB_INTP_INTX_MASK		(PAB_INTP_INTA | PAB_INTP_INTB |\
6762306a36Sopenharmony_ci					PAB_INTP_INTC | PAB_INTP_INTD)
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#define PAB_AXI_AMAP_CTRL(win)		PAB_REG_ADDR(0x0ba0, win)
7062306a36Sopenharmony_ci#define  WIN_ENABLE_SHIFT		0
7162306a36Sopenharmony_ci#define  WIN_TYPE_SHIFT			1
7262306a36Sopenharmony_ci#define  WIN_TYPE_MASK			0x3
7362306a36Sopenharmony_ci#define  WIN_SIZE_MASK			0xfffffc00
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#define PAB_EXT_AXI_AMAP_SIZE(win)	PAB_EXT_REG_ADDR(0xbaf0, win)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define PAB_EXT_AXI_AMAP_AXI_WIN(win)	PAB_EXT_REG_ADDR(0x80a0, win)
7862306a36Sopenharmony_ci#define PAB_AXI_AMAP_AXI_WIN(win)	PAB_REG_ADDR(0x0ba4, win)
7962306a36Sopenharmony_ci#define  AXI_WINDOW_ALIGN_MASK		3
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci#define PAB_AXI_AMAP_PEX_WIN_L(win)	PAB_REG_ADDR(0x0ba8, win)
8262306a36Sopenharmony_ci#define  PAB_BUS_SHIFT			24
8362306a36Sopenharmony_ci#define  PAB_DEVICE_SHIFT		19
8462306a36Sopenharmony_ci#define  PAB_FUNCTION_SHIFT		16
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci#define PAB_AXI_AMAP_PEX_WIN_H(win)	PAB_REG_ADDR(0x0bac, win)
8762306a36Sopenharmony_ci#define PAB_INTP_AXI_PIO_CLASS		0x474
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define PAB_PEX_AMAP_CTRL(win)		PAB_REG_ADDR(0x4ba0, win)
9062306a36Sopenharmony_ci#define  AMAP_CTRL_EN_SHIFT		0
9162306a36Sopenharmony_ci#define  AMAP_CTRL_TYPE_SHIFT		1
9262306a36Sopenharmony_ci#define  AMAP_CTRL_TYPE_MASK		3
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci#define PAB_EXT_PEX_AMAP_SIZEN(win)	PAB_EXT_REG_ADDR(0xbef0, win)
9562306a36Sopenharmony_ci#define PAB_EXT_PEX_AMAP_AXI_WIN(win)	PAB_EXT_REG_ADDR(0xb4a0, win)
9662306a36Sopenharmony_ci#define PAB_PEX_AMAP_AXI_WIN(win)	PAB_REG_ADDR(0x4ba4, win)
9762306a36Sopenharmony_ci#define PAB_PEX_AMAP_PEX_WIN_L(win)	PAB_REG_ADDR(0x4ba8, win)
9862306a36Sopenharmony_ci#define PAB_PEX_AMAP_PEX_WIN_H(win)	PAB_REG_ADDR(0x4bac, win)
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/* starting offset of INTX bits in status register */
10162306a36Sopenharmony_ci#define PAB_INTX_START			5
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci/* supported number of MSI interrupts */
10462306a36Sopenharmony_ci#define PCI_NUM_MSI			16
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci/* MSI registers */
10762306a36Sopenharmony_ci#define MSI_BASE_LO_OFFSET		0x04
10862306a36Sopenharmony_ci#define MSI_BASE_HI_OFFSET		0x08
10962306a36Sopenharmony_ci#define MSI_SIZE_OFFSET			0x0c
11062306a36Sopenharmony_ci#define MSI_ENABLE_OFFSET		0x14
11162306a36Sopenharmony_ci#define MSI_STATUS_OFFSET		0x18
11262306a36Sopenharmony_ci#define MSI_DATA_OFFSET			0x20
11362306a36Sopenharmony_ci#define MSI_ADDR_L_OFFSET		0x24
11462306a36Sopenharmony_ci#define MSI_ADDR_H_OFFSET		0x28
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/* outbound and inbound window definitions */
11762306a36Sopenharmony_ci#define WIN_NUM_0			0
11862306a36Sopenharmony_ci#define WIN_NUM_1			1
11962306a36Sopenharmony_ci#define CFG_WINDOW_TYPE			0
12062306a36Sopenharmony_ci#define IO_WINDOW_TYPE			1
12162306a36Sopenharmony_ci#define MEM_WINDOW_TYPE			2
12262306a36Sopenharmony_ci#define IB_WIN_SIZE			((u64)256 * 1024 * 1024 * 1024)
12362306a36Sopenharmony_ci#define MAX_PIO_WINDOWS			8
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/* Parameters for the waiting for link up routine */
12662306a36Sopenharmony_ci#define LINK_WAIT_MAX_RETRIES		10
12762306a36Sopenharmony_ci#define LINK_WAIT_MIN			90000
12862306a36Sopenharmony_ci#define LINK_WAIT_MAX			100000
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define PAGED_ADDR_BNDRY		0xc00
13162306a36Sopenharmony_ci#define OFFSET_TO_PAGE_ADDR(off)	\
13262306a36Sopenharmony_ci	((off & PAGE_LO_MASK) | PAGED_ADDR_BNDRY)
13362306a36Sopenharmony_ci#define OFFSET_TO_PAGE_IDX(off)		\
13462306a36Sopenharmony_ci	((off >> PAGE_SEL_OFFSET_SHIFT) & PAGE_SEL_MASK)
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_cistruct mobiveil_msi {			/* MSI information */
13762306a36Sopenharmony_ci	struct mutex lock;		/* protect bitmap variable */
13862306a36Sopenharmony_ci	struct irq_domain *msi_domain;
13962306a36Sopenharmony_ci	struct irq_domain *dev_domain;
14062306a36Sopenharmony_ci	phys_addr_t msi_pages_phys;
14162306a36Sopenharmony_ci	int num_of_vectors;
14262306a36Sopenharmony_ci	DECLARE_BITMAP(msi_irq_in_use, PCI_NUM_MSI);
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistruct mobiveil_pcie;
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_cistruct mobiveil_rp_ops {
14862306a36Sopenharmony_ci	int (*interrupt_init)(struct mobiveil_pcie *pcie);
14962306a36Sopenharmony_ci};
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistruct mobiveil_root_port {
15262306a36Sopenharmony_ci	void __iomem *config_axi_slave_base;	/* endpoint config base */
15362306a36Sopenharmony_ci	struct resource *ob_io_res;
15462306a36Sopenharmony_ci	struct mobiveil_rp_ops *ops;
15562306a36Sopenharmony_ci	int irq;
15662306a36Sopenharmony_ci	raw_spinlock_t intx_mask_lock;
15762306a36Sopenharmony_ci	struct irq_domain *intx_domain;
15862306a36Sopenharmony_ci	struct mobiveil_msi msi;
15962306a36Sopenharmony_ci	struct pci_host_bridge *bridge;
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistruct mobiveil_pab_ops {
16362306a36Sopenharmony_ci	int (*link_up)(struct mobiveil_pcie *pcie);
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistruct mobiveil_pcie {
16762306a36Sopenharmony_ci	struct platform_device *pdev;
16862306a36Sopenharmony_ci	void __iomem *csr_axi_slave_base;	/* root port config base */
16962306a36Sopenharmony_ci	void __iomem *apb_csr_base;	/* MSI register base */
17062306a36Sopenharmony_ci	phys_addr_t pcie_reg_base;	/* Physical PCIe Controller Base */
17162306a36Sopenharmony_ci	int apio_wins;
17262306a36Sopenharmony_ci	int ppio_wins;
17362306a36Sopenharmony_ci	int ob_wins_configured;		/* configured outbound windows */
17462306a36Sopenharmony_ci	int ib_wins_configured;		/* configured inbound windows */
17562306a36Sopenharmony_ci	const struct mobiveil_pab_ops *ops;
17662306a36Sopenharmony_ci	struct mobiveil_root_port rp;
17762306a36Sopenharmony_ci};
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciint mobiveil_pcie_host_probe(struct mobiveil_pcie *pcie);
18062306a36Sopenharmony_ciint mobiveil_host_init(struct mobiveil_pcie *pcie, bool reinit);
18162306a36Sopenharmony_cibool mobiveil_pcie_link_up(struct mobiveil_pcie *pcie);
18262306a36Sopenharmony_ciint mobiveil_bringup_link(struct mobiveil_pcie *pcie);
18362306a36Sopenharmony_civoid program_ob_windows(struct mobiveil_pcie *pcie, int win_num, u64 cpu_addr,
18462306a36Sopenharmony_ci			u64 pci_addr, u32 type, u64 size);
18562306a36Sopenharmony_civoid program_ib_windows(struct mobiveil_pcie *pcie, int win_num, u64 cpu_addr,
18662306a36Sopenharmony_ci			u64 pci_addr, u32 type, u64 size);
18762306a36Sopenharmony_ciu32 mobiveil_csr_read(struct mobiveil_pcie *pcie, u32 off, size_t size);
18862306a36Sopenharmony_civoid mobiveil_csr_write(struct mobiveil_pcie *pcie, u32 val, u32 off,
18962306a36Sopenharmony_ci			size_t size);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_cistatic inline u32 mobiveil_csr_readl(struct mobiveil_pcie *pcie, u32 off)
19262306a36Sopenharmony_ci{
19362306a36Sopenharmony_ci	return mobiveil_csr_read(pcie, off, 0x4);
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistatic inline u16 mobiveil_csr_readw(struct mobiveil_pcie *pcie, u32 off)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	return mobiveil_csr_read(pcie, off, 0x2);
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic inline u8 mobiveil_csr_readb(struct mobiveil_pcie *pcie, u32 off)
20262306a36Sopenharmony_ci{
20362306a36Sopenharmony_ci	return mobiveil_csr_read(pcie, off, 0x1);
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic inline void mobiveil_csr_writel(struct mobiveil_pcie *pcie, u32 val,
20862306a36Sopenharmony_ci				       u32 off)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	mobiveil_csr_write(pcie, val, off, 0x4);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic inline void mobiveil_csr_writew(struct mobiveil_pcie *pcie, u16 val,
21462306a36Sopenharmony_ci				       u32 off)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	mobiveil_csr_write(pcie, val, off, 0x2);
21762306a36Sopenharmony_ci}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistatic inline void mobiveil_csr_writeb(struct mobiveil_pcie *pcie, u8 val,
22062306a36Sopenharmony_ci				       u32 off)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	mobiveil_csr_write(pcie, val, off, 0x1);
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci#endif /* _PCIE_MOBIVEIL_H */
226