blob: 97859a0cb87e9f144ad75405fc9342260ad80a34 [file] [log] [blame]
Srinath Mannam75687022020-04-02 16:08:12 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 Broadcom
4 */
5
Srinath Mannam75687022020-04-02 16:08:12 +05306#include <dm.h>
7#include <generic-phy.h>
8#include <asm/io.h>
9#include <linux/bitops.h>
10
11/* we have up to 8 PAXB based RC. The 9th one is always PAXC */
12#define SR_NR_PCIE_PHYS 8
13
14#define PCIE_PIPEMUX_CFG_OFFSET 0x10c
15#define PCIE_PIPEMUX_SELECT_STRAP GENMASK(3, 0)
16
17#define CDRU_STRAP_DATA_LSW_OFFSET 0x5c
18#define PCIE_PIPEMUX_SHIFT 19
19#define PCIE_PIPEMUX_MASK GENMASK(3, 0)
20
21/**
22 * struct sr_pcie_phy_core - Stingray PCIe PHY core control
23 *
24 * @dev: pointer to device
25 * @base: base register of PCIe SS
26 * @cdru: CDRU base address
27 * @pipemux: pipemuex strap
28 */
29struct sr_pcie_phy_core {
30 struct udevice *dev;
31 void __iomem *base;
32 void __iomem *cdru;
33 u32 pipemux;
34};
35
36/*
37 * PCIe PIPEMUX lookup table
38 *
39 * Each array index represents a PIPEMUX strap setting
40 * The array element represents a bitmap where a set bit means the PCIe
41 * core and associated serdes has been enabled as RC and is available for use
42 */
43static const u8 pipemux_table[] = {
44 /* PIPEMUX = 0, EP 1x16 */
45 0x00,
46 /* PIPEMUX = 1, EP 1x8 + RC 1x8, core 7 */
47 0x80,
48 /* PIPEMUX = 2, EP 4x4 */
49 0x00,
50 /* PIPEMUX = 3, RC 2x8, cores 0, 7 */
51 0x81,
52 /* PIPEMUX = 4, RC 4x4, cores 0, 1, 6, 7 */
53 0xc3,
54 /* PIPEMUX = 5, RC 8x2, all 8 cores */
55 0xff,
56 /* PIPEMUX = 6, RC 3x4 + 2x2, cores 0, 2, 3, 6, 7 */
57 0xcd,
58 /* PIPEMUX = 7, RC 1x4 + 6x2, cores 0, 2, 3, 4, 5, 6, 7 */
59 0xfd,
60 /* PIPEMUX = 8, EP 1x8 + RC 4x2, cores 4, 5, 6, 7 */
61 0xf0,
62 /* PIPEMUX = 9, EP 1x8 + RC 2x4, cores 6, 7 */
63 0xc0,
64 /* PIPEMUX = 10, EP 2x4 + RC 2x4, cores 1, 6 */
65 0x42,
66 /* PIPEMUX = 11, EP 2x4 + RC 4x2, cores 2, 3, 4, 5 */
67 0x3c,
68 /* PIPEMUX = 12, EP 1x4 + RC 6x2, cores 2, 3, 4, 5, 6, 7 */
69 0xfc,
70 /* PIPEMUX = 13, RC 2x4 + RC 1x4 + 2x2, cores 2, 3, 6 */
71 0x4c,
72};
73
74/*
75 * Return true if the strap setting is valid
76 */
77static bool pipemux_strap_is_valid(u32 pipemux)
78{
79 return !!(pipemux < ARRAY_SIZE(pipemux_table));
80}
81
82/*
83 * Read the PCIe PIPEMUX from strap
84 */
85static u32 pipemux_strap_read(struct sr_pcie_phy_core *core)
86{
87 u32 pipemux;
88
89 /*
90 * Read PIPEMUX configuration register to determine the pipemux setting
91 *
92 * In the case when the value indicates using HW strap, fall back to
93 * use HW strap
94 */
95 pipemux = readl(core->base + PCIE_PIPEMUX_CFG_OFFSET);
96 pipemux &= PCIE_PIPEMUX_MASK;
97 if (pipemux == PCIE_PIPEMUX_SELECT_STRAP) {
98 pipemux = readl(core->cdru + CDRU_STRAP_DATA_LSW_OFFSET);
99 pipemux >>= PCIE_PIPEMUX_SHIFT;
100 pipemux &= PCIE_PIPEMUX_MASK;
101 }
102
103 return pipemux;
104}
105
106static int sr_pcie_phy_init(struct phy *phy)
107{
108 struct sr_pcie_phy_core *core = dev_get_priv(phy->dev);
109 unsigned int core_idx = phy->id;
110
111 debug("%s %lx\n", __func__, phy->id);
112 /*
113 * Check whether this PHY is for root complex or not. If yes, return
114 * zero so the host driver can proceed to enumeration. If not, return
115 * an error and that will force the host driver to bail out
116 */
117 if (!!((pipemux_table[core->pipemux] >> core_idx) & 0x1))
118 return 0;
119
120 return -ENODEV;
121}
122
123static int sr_pcie_phy_xlate(struct phy *phy, struct ofnode_phandle_args *args)
124{
125 debug("%s %d\n", __func__, args->args[0]);
126 if (args->args_count && args->args[0] < SR_NR_PCIE_PHYS)
127 phy->id = args->args[0];
128 else
129 return -ENODEV;
130
131 return 0;
132}
133
134static const struct phy_ops sr_pcie_phy_ops = {
135 .of_xlate = sr_pcie_phy_xlate,
136 .init = sr_pcie_phy_init,
137};
138
139static int sr_pcie_phy_probe(struct udevice *dev)
140{
141 struct sr_pcie_phy_core *core = dev_get_priv(dev);
142
143 core->dev = dev;
144
Matthias Schiffer47331932023-09-27 15:33:34 +0200145 core->base = (void __iomem *)dev_read_addr_name_ptr(dev, "reg_base");
146 core->cdru = (void __iomem *)dev_read_addr_name_ptr(dev, "cdru_base");
Srinath Mannam75687022020-04-02 16:08:12 +0530147 debug("ip base %p\n", core->base);
148 debug("cdru base %p\n", core->cdru);
149
150 /* read the PCIe PIPEMUX strap setting */
151 core->pipemux = pipemux_strap_read(core);
152 if (!pipemux_strap_is_valid(core->pipemux)) {
153 pr_err("invalid PCIe PIPEMUX strap %u\n", core->pipemux);
154 return -EIO;
155 }
156 debug("%s %#x\n", __func__, core->pipemux);
157
158 pr_info("Stingray PCIe PHY driver initialized\n");
159
160 return 0;
161}
162
163static const struct udevice_id sr_pcie_phy_match_table[] = {
164 { .compatible = "brcm,sr-pcie-phy" },
165 { }
166};
167
168U_BOOT_DRIVER(sr_pcie_phy) = {
169 .name = "sr-pcie-phy",
170 .id = UCLASS_PHY,
171 .probe = sr_pcie_phy_probe,
172 .of_match = sr_pcie_phy_match_table,
173 .ops = &sr_pcie_phy_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700174 .plat_auto = sizeof(struct sr_pcie_phy_core),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700175 .priv_auto = sizeof(struct sr_pcie_phy_core),
Srinath Mannam75687022020-04-02 16:08:12 +0530176};