blob: 673813b8b755965a2b84616b475e82fff5f64e65 [file] [log] [blame]
John Crispin1a753002024-06-24 23:03:28 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek PCIe host controller driver.
4 *
5 * Copyright (c) 2023 John Crispin <john@phrozen.org>
6 * Driver is based on u-boot gen1/2 and upstream linux gen3 code
7 */
8
9#include <clk.h>
10#include <dm.h>
11#include <generic-phy.h>
12#include <log.h>
13#include <malloc.h>
14#include <pci.h>
15#include <reset.h>
16#include <asm/io.h>
17#include <dm/device_compat.h>
18#include <dm/devres.h>
19#include <linux/bitops.h>
20#include <linux/iopoll.h>
21#include <linux/list.h>
22#include "pci_internal.h"
23
24/* PCIe shared registers */
25#define PCIE_CFG_ADDR 0x20
26#define PCIE_CFG_DATA 0x24
27
28#define PCIE_SETTING_REG 0x80
29
30#define PCIE_PCI_IDS_1 0x9c
31#define PCIE_RC_MODE BIT(0)
32#define PCI_CLASS(class) ((class) << 8)
33
34#define PCIE_CFGNUM_REG 0x140
35#define PCIE_CFG_DEVFN(devfn) ((devfn) & GENMASK(7, 0))
36#define PCIE_CFG_BUS(bus) (((bus) << 8) & GENMASK(15, 8))
37#define PCIE_CFG_BYTE_EN(bytes) (((bytes) << 16) & GENMASK(19, 16))
38#define PCIE_CFG_FORCE_BYTE_EN BIT(20)
39#define PCIE_CFG_OFFSET_ADDR 0x1000
40#define PCIE_CFG_HEADER(bus, devfn) (PCIE_CFG_BUS(bus) | PCIE_CFG_DEVFN(devfn))
41
42#define PCIE_RST_CTRL_REG 0x148
43#define PCIE_MAC_RSTB BIT(0)
44#define PCIE_PHY_RSTB BIT(1)
45#define PCIE_BRG_RSTB BIT(2)
46#define PCIE_PE_RSTB BIT(3)
47
48#define PCIE_LINK_STATUS_REG 0x154
49#define PCIE_PORT_LINKUP BIT(8)
50
51#define PCIE_INT_ENABLE_REG 0x180
52
53#define PCIE_MISC_CTRL_REG 0x348
54#define PCIE_DISABLE_DVFSRC_VLT_REQ BIT(1)
55
56#define PCIE_TRANS_TABLE_BASE_REG 0x800
57#define PCIE_ATR_SRC_ADDR_MSB_OFFSET 0x4
58#define PCIE_ATR_TRSL_ADDR_LSB_OFFSET 0x8
59#define PCIE_ATR_TRSL_ADDR_MSB_OFFSET 0xc
60#define PCIE_ATR_TRSL_PARAM_OFFSET 0x10
61#define PCIE_ATR_TLB_SET_OFFSET 0x20
62
63#define PCIE_MAX_TRANS_TABLES 8
64#define PCIE_ATR_EN BIT(0)
65#define PCIE_ATR_SIZE(size) \
66 (((((size) - 1) << 1) & GENMASK(6, 1)) | PCIE_ATR_EN)
67#define PCIE_ATR_ID(id) ((id) & GENMASK(3, 0))
68#define PCIE_ATR_TYPE_MEM PCIE_ATR_ID(0)
69#define PCIE_ATR_TYPE_IO PCIE_ATR_ID(1)
70#define PCIE_ATR_TLP_TYPE(type) (((type) << 16) & GENMASK(18, 16))
71#define PCIE_ATR_TLP_TYPE_MEM PCIE_ATR_TLP_TYPE(0)
72#define PCIE_ATR_TLP_TYPE_IO PCIE_ATR_TLP_TYPE(2)
73
74struct mtk_pcie {
75 void __iomem *base;
76 void *priv;
77 struct clk pl_250m_ck;
78 struct clk tl_26m_ck;
79 struct clk peri_26m_ck;
80 struct clk top_133m_ck;
81 struct reset_ctl reset_phy;
82 struct reset_ctl reset_mac;
83 struct phy phy;
84};
85
86static void mtk_pcie_config_tlp_header(const struct udevice *bus,
87 pci_dev_t devfn,
88 int where, int size)
89{
90 struct mtk_pcie *pcie = dev_get_priv(bus);
91 int bytes;
92 u32 val;
93
94 size = 1 << size;
95 bytes = (GENMASK(size - 1, 0) & 0xf) << (where & 0x3);
96
97 val = PCIE_CFG_FORCE_BYTE_EN | PCIE_CFG_BYTE_EN(bytes) |
98 PCIE_CFG_HEADER(PCI_BUS(devfn), (devfn >> 8));
99
100 writel(val, pcie->base + PCIE_CFGNUM_REG);
101}
102
103static int mtk_pcie_config_address(const struct udevice *udev, pci_dev_t bdf,
104 uint offset, void **paddress)
105{
106 struct mtk_pcie *pcie = dev_get_priv(udev);
107
108 *paddress = pcie->base + PCIE_CFG_OFFSET_ADDR + offset;
109
110 return 0;
111}
112
113static int mtk_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
114 uint offset, ulong *valuep,
115 enum pci_size_t size)
116{
117 int ret;
118
119 mtk_pcie_config_tlp_header(bus, bdf, offset, size);
120 ret = pci_generic_mmap_read_config(bus, mtk_pcie_config_address,
121 bdf, offset, valuep, size);
122 return ret;
123}
124
125static int mtk_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
126 uint offset, ulong value,
127 enum pci_size_t size)
128{
129 mtk_pcie_config_tlp_header(bus, bdf, offset, size);
130
131 switch (size) {
132 case PCI_SIZE_8:
133 case PCI_SIZE_16:
134 value <<= (offset & 0x3) * 8;
135 case PCI_SIZE_32:
136 break;
137 default:
138 return -EINVAL;
139 }
140
141 return pci_generic_mmap_write_config(bus, mtk_pcie_config_address,
142 bdf, (offset & ~0x3), value, PCI_SIZE_32);
143}
144
145static const struct dm_pci_ops mtk_pcie_ops = {
146 .read_config = mtk_pcie_read_config,
147 .write_config = mtk_pcie_write_config,
148};
149
150static int mtk_pcie_set_trans_table(struct udevice *dev, struct mtk_pcie *pcie,
151 u64 cpu_addr, u64 pci_addr, u64 size,
152 unsigned long type, int num)
153{
154 void __iomem *table;
155 u32 val;
156
157 if (num >= PCIE_MAX_TRANS_TABLES) {
158 dev_err(dev, "not enough translate table for addr: %#llx, limited to [%d]\n",
159 (unsigned long long)cpu_addr, PCIE_MAX_TRANS_TABLES);
160 return -ENODEV;
161 }
162
163 dev_dbg(dev, "set trans table %d: %#llx %#llx, %#llx\n", num, cpu_addr,
164 pci_addr, size);
165 table = pcie->base + PCIE_TRANS_TABLE_BASE_REG +
166 num * PCIE_ATR_TLB_SET_OFFSET;
167
168 writel(lower_32_bits(cpu_addr) | PCIE_ATR_SIZE(fls(size) - 1), table);
169 writel(upper_32_bits(cpu_addr), table + PCIE_ATR_SRC_ADDR_MSB_OFFSET);
170 writel(lower_32_bits(pci_addr), table + PCIE_ATR_TRSL_ADDR_LSB_OFFSET);
171 writel(upper_32_bits(pci_addr), table + PCIE_ATR_TRSL_ADDR_MSB_OFFSET);
172
173 if (type == PCI_REGION_IO)
174 val = PCIE_ATR_TYPE_IO | PCIE_ATR_TLP_TYPE_IO;
175 else
176 val = PCIE_ATR_TYPE_MEM | PCIE_ATR_TLP_TYPE_MEM;
177 writel(val, table + PCIE_ATR_TRSL_PARAM_OFFSET);
178
179 return 0;
180}
181
182static int mtk_pcie_startup_port(struct udevice *dev)
183{
184 struct mtk_pcie *pcie = dev_get_priv(dev);
185 struct udevice *ctlr = pci_get_controller(dev);
186 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
187 u32 val;
188 int i, err;
189
190 /* Set as RC mode */
191 val = readl(pcie->base + PCIE_SETTING_REG);
192 val |= PCIE_RC_MODE;
193 writel(val, pcie->base + PCIE_SETTING_REG);
194
195 /* setup RC BARs */
196 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
197 pcie->base + PCI_BASE_ADDRESS_0);
198 writel(0x0, pcie->base + PCI_BASE_ADDRESS_1);
199
200 /* setup interrupt pins */
201 clrsetbits_le32(pcie->base + PCI_INTERRUPT_LINE,
202 0xff00, 0x100);
203
204 /* setup bus numbers */
205 clrsetbits_le32(pcie->base + PCI_PRIMARY_BUS,
206 0xffffff, 0x00ff0100);
207
208 /* setup command register */
209 clrsetbits_le32(pcie->base + PCI_PRIMARY_BUS,
210 0xffff,
211 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
212 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
213
214 /* Set class code */
215 val = readl(pcie->base + PCIE_PCI_IDS_1);
216 val &= ~GENMASK(31, 8);
217 val |= PCI_CLASS(PCI_CLASS_BRIDGE_PCI << 8);
218 writel(val, pcie->base + PCIE_PCI_IDS_1);
219
220 /* Mask all INTx interrupts */
221 val = readl(pcie->base + PCIE_INT_ENABLE_REG);
222 val &= ~0xFF000000;
223 writel(val, pcie->base + PCIE_INT_ENABLE_REG);
224
225 /* Disable DVFSRC voltage request */
226 val = readl(pcie->base + PCIE_MISC_CTRL_REG);
227 val |= PCIE_DISABLE_DVFSRC_VLT_REQ;
228 writel(val, pcie->base + PCIE_MISC_CTRL_REG);
229
230 /* Assert all reset signals */
231 val = readl(pcie->base + PCIE_RST_CTRL_REG);
232 val |= PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB | PCIE_PE_RSTB;
233 writel(val, pcie->base + PCIE_RST_CTRL_REG);
234
235 /*
236 * Described in PCIe CEM specification sections 2.2 (PERST# Signal)
237 * and 2.2.1 (Initial Power-Up (G3 to S0)).
238 * The deassertion of PERST# should be delayed 100ms (TPVPERL)
239 * for the power and clock to become stable.
240 */
241 mdelay(100);
242
243 /* De-assert reset signals */
244 val &= ~(PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB);
245 writel(val, pcie->base + PCIE_RST_CTRL_REG);
246
247 mdelay(100);
248
249 /* De-assert PERST# signals */
250 val &= ~(PCIE_PE_RSTB);
251 writel(val, pcie->base + PCIE_RST_CTRL_REG);
252
253 /* 100ms timeout value should be enough for Gen1/2 training */
254 err = readl_poll_timeout(pcie->base + PCIE_LINK_STATUS_REG, val,
255 !!(val & PCIE_PORT_LINKUP),
256 100 * 1000);
257 if (err) {
258 dev_dbg(dev, "no card detected\n");
259 return -ETIMEDOUT;
260 }
261 dev_dbg(dev, "detected a card\n");
262
263 for (i = 0; i < hose->region_count; i++) {
264 struct pci_region *reg = &hose->regions[i];
265
266 if (reg->flags != PCI_REGION_MEM)
267 continue;
268
269 mtk_pcie_set_trans_table(dev, pcie, reg->bus_start, reg->phys_start,
270 reg->size, reg->flags, 0);
271 }
272
273 return 0;
274}
275
276static int mtk_pcie_power_on(struct udevice *dev)
277{
278 struct mtk_pcie *pcie = dev_get_priv(dev);
279 int err;
280
281 pcie->base = dev_remap_addr_name(dev, "pcie-mac");
282 if (!pcie->base)
283 return -ENOENT;
284
285 pcie->priv = dev;
286
287 err = generic_phy_get_by_name(dev, "pcie-phy", &pcie->phy);
288 if (err)
289 return err;
290
291 /*
292 * Upstream linux kernel devine these clock without clock-names
293 * and use clk bulk API to enable them all.
294 */
295 err = clk_get_by_index(dev, 0, &pcie->pl_250m_ck);
296 if (err)
297 return err;
298
299 err = clk_get_by_index(dev, 1, &pcie->tl_26m_ck);
300 if (err)
301 return err;
302
303 err = clk_get_by_index(dev, 2, &pcie->peri_26m_ck);
304 if (err)
305 return err;
306
307 err = clk_get_by_index(dev, 3, &pcie->top_133m_ck);
308 if (err)
309 return err;
310
311 err = generic_phy_init(&pcie->phy);
312 if (err)
313 return err;
314
315 err = generic_phy_power_on(&pcie->phy);
316 if (err)
317 goto err_phy_on;
318
319 err = clk_enable(&pcie->pl_250m_ck);
320 if (err)
321 goto err_clk_pl_250m;
322
323 err = clk_enable(&pcie->tl_26m_ck);
324 if (err)
325 goto err_clk_tl_26m;
326
327 err = clk_enable(&pcie->peri_26m_ck);
328 if (err)
329 goto err_clk_peri_26m;
330
331 err = clk_enable(&pcie->top_133m_ck);
332 if (err)
333 goto err_clk_top_133m;
334
335 err = mtk_pcie_startup_port(dev);
336 if (err)
337 goto err_startup;
338
339 return 0;
340
341err_startup:
342err_clk_top_133m:
343 clk_disable(&pcie->top_133m_ck);
344err_clk_peri_26m:
345 clk_disable(&pcie->peri_26m_ck);
346err_clk_tl_26m:
347 clk_disable(&pcie->tl_26m_ck);
348err_clk_pl_250m:
349 clk_disable(&pcie->pl_250m_ck);
350err_phy_on:
351 generic_phy_exit(&pcie->phy);
352
353 return err;
354}
355
356static int mtk_pcie_probe(struct udevice *dev)
357{
358 struct mtk_pcie *pcie = dev_get_priv(dev);
359 int err;
360
361 pcie->priv = dev;
362
363 err = mtk_pcie_power_on(dev);
364 if (err)
365 return err;
366
367 return 0;
368}
369
370static const struct udevice_id mtk_pcie_ids[] = {
371 { .compatible = "mediatek,mt8192-pcie" },
372 { }
373};
374
375U_BOOT_DRIVER(pcie_mediatek_gen3) = {
376 .name = "pcie_mediatek_gen3",
377 .id = UCLASS_PCI,
378 .of_match = mtk_pcie_ids,
379 .ops = &mtk_pcie_ops,
380 .probe = mtk_pcie_probe,
381 .priv_auto = sizeof(struct mtk_pcie),
382};