blob: 1818d4c1e30c66d4935ce82bb211d5b49fe723f9 [file] [log] [blame]
John Crispin651d59f2024-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
developer9b5c0f92025-01-17 17:18:11 +080086static pci_dev_t convert_bdf(const struct udevice *controller, pci_dev_t bdf)
87{
88 int bdfs[3];
89
90 bdfs[0] = PCI_BUS(bdf);
91 bdfs[1] = PCI_DEV(bdf);
92 bdfs[2] = PCI_FUNC(bdf);
93
94 /*
95 * One MediaTek PCIe Gen3 controller has only one port, where PCI bus 0 on
96 * this port represents the controller itself and bus 1 represents the
97 * external PCIe device. If multiple PCIe controllers are probed in U-Boot,
98 * U-Boot will use bus numbers greater than 2 as input parameters. Therefore,
99 * we should convert the BDF bus number to either 0 or 1 by subtracting the
100 * offset by controller->seq_
101 */
102
103 bdfs[0] = bdfs[0] - controller->seq_;
104
105 return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
106}
107
John Crispin651d59f2024-06-24 23:03:28 +0200108static void mtk_pcie_config_tlp_header(const struct udevice *bus,
109 pci_dev_t devfn,
110 int where, int size)
111{
112 struct mtk_pcie *pcie = dev_get_priv(bus);
113 int bytes;
114 u32 val;
115
developer9b5c0f92025-01-17 17:18:11 +0800116 devfn = convert_bdf(bus, devfn);
117
John Crispin651d59f2024-06-24 23:03:28 +0200118 size = 1 << size;
119 bytes = (GENMASK(size - 1, 0) & 0xf) << (where & 0x3);
120
121 val = PCIE_CFG_FORCE_BYTE_EN | PCIE_CFG_BYTE_EN(bytes) |
122 PCIE_CFG_HEADER(PCI_BUS(devfn), (devfn >> 8));
123
124 writel(val, pcie->base + PCIE_CFGNUM_REG);
125}
126
127static int mtk_pcie_config_address(const struct udevice *udev, pci_dev_t bdf,
128 uint offset, void **paddress)
129{
130 struct mtk_pcie *pcie = dev_get_priv(udev);
131
132 *paddress = pcie->base + PCIE_CFG_OFFSET_ADDR + offset;
133
134 return 0;
135}
136
137static int mtk_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
138 uint offset, ulong *valuep,
139 enum pci_size_t size)
140{
141 int ret;
142
143 mtk_pcie_config_tlp_header(bus, bdf, offset, size);
144 ret = pci_generic_mmap_read_config(bus, mtk_pcie_config_address,
145 bdf, offset, valuep, size);
146 return ret;
147}
148
149static int mtk_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
150 uint offset, ulong value,
151 enum pci_size_t size)
152{
153 mtk_pcie_config_tlp_header(bus, bdf, offset, size);
154
155 switch (size) {
156 case PCI_SIZE_8:
157 case PCI_SIZE_16:
158 value <<= (offset & 0x3) * 8;
159 case PCI_SIZE_32:
160 break;
161 default:
162 return -EINVAL;
163 }
164
165 return pci_generic_mmap_write_config(bus, mtk_pcie_config_address,
166 bdf, (offset & ~0x3), value, PCI_SIZE_32);
167}
168
169static const struct dm_pci_ops mtk_pcie_ops = {
170 .read_config = mtk_pcie_read_config,
171 .write_config = mtk_pcie_write_config,
172};
173
174static int mtk_pcie_set_trans_table(struct udevice *dev, struct mtk_pcie *pcie,
175 u64 cpu_addr, u64 pci_addr, u64 size,
176 unsigned long type, int num)
177{
178 void __iomem *table;
179 u32 val;
180
181 if (num >= PCIE_MAX_TRANS_TABLES) {
182 dev_err(dev, "not enough translate table for addr: %#llx, limited to [%d]\n",
183 (unsigned long long)cpu_addr, PCIE_MAX_TRANS_TABLES);
184 return -ENODEV;
185 }
186
187 dev_dbg(dev, "set trans table %d: %#llx %#llx, %#llx\n", num, cpu_addr,
188 pci_addr, size);
189 table = pcie->base + PCIE_TRANS_TABLE_BASE_REG +
190 num * PCIE_ATR_TLB_SET_OFFSET;
191
192 writel(lower_32_bits(cpu_addr) | PCIE_ATR_SIZE(fls(size) - 1), table);
193 writel(upper_32_bits(cpu_addr), table + PCIE_ATR_SRC_ADDR_MSB_OFFSET);
194 writel(lower_32_bits(pci_addr), table + PCIE_ATR_TRSL_ADDR_LSB_OFFSET);
195 writel(upper_32_bits(pci_addr), table + PCIE_ATR_TRSL_ADDR_MSB_OFFSET);
196
197 if (type == PCI_REGION_IO)
198 val = PCIE_ATR_TYPE_IO | PCIE_ATR_TLP_TYPE_IO;
199 else
200 val = PCIE_ATR_TYPE_MEM | PCIE_ATR_TLP_TYPE_MEM;
201 writel(val, table + PCIE_ATR_TRSL_PARAM_OFFSET);
202
203 return 0;
204}
205
206static int mtk_pcie_startup_port(struct udevice *dev)
207{
208 struct mtk_pcie *pcie = dev_get_priv(dev);
209 struct udevice *ctlr = pci_get_controller(dev);
210 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
211 u32 val;
212 int i, err;
213
214 /* Set as RC mode */
215 val = readl(pcie->base + PCIE_SETTING_REG);
216 val |= PCIE_RC_MODE;
217 writel(val, pcie->base + PCIE_SETTING_REG);
218
219 /* setup RC BARs */
220 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
221 pcie->base + PCI_BASE_ADDRESS_0);
222 writel(0x0, pcie->base + PCI_BASE_ADDRESS_1);
223
224 /* setup interrupt pins */
225 clrsetbits_le32(pcie->base + PCI_INTERRUPT_LINE,
226 0xff00, 0x100);
227
228 /* setup bus numbers */
229 clrsetbits_le32(pcie->base + PCI_PRIMARY_BUS,
230 0xffffff, 0x00ff0100);
231
232 /* setup command register */
233 clrsetbits_le32(pcie->base + PCI_PRIMARY_BUS,
234 0xffff,
235 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
236 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
237
238 /* Set class code */
239 val = readl(pcie->base + PCIE_PCI_IDS_1);
240 val &= ~GENMASK(31, 8);
241 val |= PCI_CLASS(PCI_CLASS_BRIDGE_PCI << 8);
242 writel(val, pcie->base + PCIE_PCI_IDS_1);
243
244 /* Mask all INTx interrupts */
245 val = readl(pcie->base + PCIE_INT_ENABLE_REG);
246 val &= ~0xFF000000;
247 writel(val, pcie->base + PCIE_INT_ENABLE_REG);
248
249 /* Disable DVFSRC voltage request */
250 val = readl(pcie->base + PCIE_MISC_CTRL_REG);
251 val |= PCIE_DISABLE_DVFSRC_VLT_REQ;
252 writel(val, pcie->base + PCIE_MISC_CTRL_REG);
253
254 /* Assert all reset signals */
255 val = readl(pcie->base + PCIE_RST_CTRL_REG);
256 val |= PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB | PCIE_PE_RSTB;
257 writel(val, pcie->base + PCIE_RST_CTRL_REG);
258
259 /*
260 * Described in PCIe CEM specification sections 2.2 (PERST# Signal)
261 * and 2.2.1 (Initial Power-Up (G3 to S0)).
262 * The deassertion of PERST# should be delayed 100ms (TPVPERL)
263 * for the power and clock to become stable.
264 */
265 mdelay(100);
266
267 /* De-assert reset signals */
268 val &= ~(PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB);
269 writel(val, pcie->base + PCIE_RST_CTRL_REG);
270
271 mdelay(100);
272
273 /* De-assert PERST# signals */
274 val &= ~(PCIE_PE_RSTB);
275 writel(val, pcie->base + PCIE_RST_CTRL_REG);
276
277 /* 100ms timeout value should be enough for Gen1/2 training */
278 err = readl_poll_timeout(pcie->base + PCIE_LINK_STATUS_REG, val,
279 !!(val & PCIE_PORT_LINKUP),
280 100 * 1000);
281 if (err) {
282 dev_dbg(dev, "no card detected\n");
283 return -ETIMEDOUT;
284 }
285 dev_dbg(dev, "detected a card\n");
286
287 for (i = 0; i < hose->region_count; i++) {
288 struct pci_region *reg = &hose->regions[i];
289
290 if (reg->flags != PCI_REGION_MEM)
291 continue;
292
293 mtk_pcie_set_trans_table(dev, pcie, reg->bus_start, reg->phys_start,
294 reg->size, reg->flags, 0);
295 }
296
297 return 0;
298}
299
300static int mtk_pcie_power_on(struct udevice *dev)
301{
302 struct mtk_pcie *pcie = dev_get_priv(dev);
303 int err;
304
305 pcie->base = dev_remap_addr_name(dev, "pcie-mac");
306 if (!pcie->base)
307 return -ENOENT;
308
309 pcie->priv = dev;
310
Christian Marangi5b5657d2024-06-24 23:03:38 +0200311 /* pcie-phy is optional (mt7988 doesn't need it) */
312 generic_phy_get_by_name(dev, "pcie-phy", &pcie->phy);
John Crispin651d59f2024-06-24 23:03:28 +0200313
314 /*
315 * Upstream linux kernel devine these clock without clock-names
316 * and use clk bulk API to enable them all.
317 */
318 err = clk_get_by_index(dev, 0, &pcie->pl_250m_ck);
319 if (err)
320 return err;
321
322 err = clk_get_by_index(dev, 1, &pcie->tl_26m_ck);
323 if (err)
324 return err;
325
326 err = clk_get_by_index(dev, 2, &pcie->peri_26m_ck);
327 if (err)
328 return err;
329
330 err = clk_get_by_index(dev, 3, &pcie->top_133m_ck);
331 if (err)
332 return err;
333
Christian Marangi5b5657d2024-06-24 23:03:38 +0200334 if (pcie->phy.dev) {
335 err = generic_phy_init(&pcie->phy);
336 if (err)
337 return err;
John Crispin651d59f2024-06-24 23:03:28 +0200338
Christian Marangi5b5657d2024-06-24 23:03:38 +0200339 err = generic_phy_power_on(&pcie->phy);
340 if (err)
341 goto err_phy_on;
342 }
John Crispin651d59f2024-06-24 23:03:28 +0200343
344 err = clk_enable(&pcie->pl_250m_ck);
345 if (err)
346 goto err_clk_pl_250m;
347
348 err = clk_enable(&pcie->tl_26m_ck);
349 if (err)
350 goto err_clk_tl_26m;
351
352 err = clk_enable(&pcie->peri_26m_ck);
353 if (err)
354 goto err_clk_peri_26m;
355
356 err = clk_enable(&pcie->top_133m_ck);
357 if (err)
358 goto err_clk_top_133m;
359
360 err = mtk_pcie_startup_port(dev);
361 if (err)
362 goto err_startup;
363
364 return 0;
365
366err_startup:
367err_clk_top_133m:
368 clk_disable(&pcie->top_133m_ck);
369err_clk_peri_26m:
370 clk_disable(&pcie->peri_26m_ck);
371err_clk_tl_26m:
372 clk_disable(&pcie->tl_26m_ck);
373err_clk_pl_250m:
374 clk_disable(&pcie->pl_250m_ck);
375err_phy_on:
Christian Marangi5b5657d2024-06-24 23:03:38 +0200376 if (pcie->phy.dev)
377 generic_phy_exit(&pcie->phy);
John Crispin651d59f2024-06-24 23:03:28 +0200378
379 return err;
380}
381
382static int mtk_pcie_probe(struct udevice *dev)
383{
384 struct mtk_pcie *pcie = dev_get_priv(dev);
385 int err;
386
387 pcie->priv = dev;
388
389 err = mtk_pcie_power_on(dev);
390 if (err)
391 return err;
392
393 return 0;
394}
395
396static const struct udevice_id mtk_pcie_ids[] = {
397 { .compatible = "mediatek,mt8192-pcie" },
398 { }
399};
400
401U_BOOT_DRIVER(pcie_mediatek_gen3) = {
402 .name = "pcie_mediatek_gen3",
403 .id = UCLASS_PCI,
404 .of_match = mtk_pcie_ids,
405 .ops = &mtk_pcie_ops,
406 .probe = mtk_pcie_probe,
407 .priv_auto = sizeof(struct mtk_pcie),
408};