blob: 6e6375cec095a686875ef47d08f55846577a4567 [file] [log] [blame]
Ryder Lee42d37452019-08-22 12:26:49 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek PCIe host controller driver.
4 *
5 * Copyright (c) 2017-2019 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
7 * Honghui Zhang <honghui.zhang@mediatek.com>
8 */
9
10#include <common.h>
11#include <clk.h>
12#include <dm.h>
13#include <generic-phy.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Ryder Lee42d37452019-08-22 12:26:49 +020016#include <pci.h>
17#include <reset.h>
18#include <asm/io.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <dm/devres.h>
Simon Glasscd93d622020-05-10 11:40:13 -060020#include <linux/bitops.h>
Ryder Lee42d37452019-08-22 12:26:49 +020021#include <linux/iopoll.h>
22#include <linux/list.h>
23
24/* PCIe shared registers */
25#define PCIE_SYS_CFG 0x00
26#define PCIE_INT_ENABLE 0x0c
27#define PCIE_CFG_ADDR 0x20
28#define PCIE_CFG_DATA 0x24
29
30/* PCIe per port registers */
31#define PCIE_BAR0_SETUP 0x10
32#define PCIE_CLASS 0x34
33#define PCIE_LINK_STATUS 0x50
34
35#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
36#define PCIE_PORT_PERST(x) BIT(1 + (x))
37#define PCIE_PORT_LINKUP BIT(0)
38#define PCIE_BAR_MAP_MAX GENMASK(31, 16)
39
40#define PCIE_BAR_ENABLE BIT(0)
41#define PCIE_REVISION_ID BIT(0)
42#define PCIE_CLASS_CODE (0x60400 << 8)
43#define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
44 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
45#define PCIE_CONF_ADDR(regn, bdf) \
46 (PCIE_CONF_REG(regn) | (bdf))
47
48/* MediaTek specific configuration registers */
49#define PCIE_FTS_NUM 0x70c
50#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
51#define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
52
53#define PCIE_FC_CREDIT 0x73c
54#define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
55#define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
56
57struct mtk_pcie_port {
58 void __iomem *base;
59 struct list_head list;
60 struct mtk_pcie *pcie;
61 struct reset_ctl reset;
62 struct clk sys_ck;
63 struct phy phy;
64 u32 slot;
65};
66
67struct mtk_pcie {
68 void __iomem *base;
69 struct clk free_ck;
70 struct list_head ports;
71};
72
Simon Glassc4e72c42020-01-27 08:49:37 -070073static int mtk_pcie_config_address(const struct udevice *udev, pci_dev_t bdf,
Ryder Lee42d37452019-08-22 12:26:49 +020074 uint offset, void **paddress)
75{
76 struct mtk_pcie *pcie = dev_get_priv(udev);
77
78 writel(PCIE_CONF_ADDR(offset, bdf), pcie->base + PCIE_CFG_ADDR);
79 *paddress = pcie->base + PCIE_CFG_DATA + (offset & 3);
80
81 return 0;
82}
83
Simon Glassc4e72c42020-01-27 08:49:37 -070084static int mtk_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Ryder Lee42d37452019-08-22 12:26:49 +020085 uint offset, ulong *valuep,
86 enum pci_size_t size)
87{
88 return pci_generic_mmap_read_config(bus, mtk_pcie_config_address,
89 bdf, offset, valuep, size);
90}
91
92static int mtk_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
93 uint offset, ulong value,
94 enum pci_size_t size)
95{
96 return pci_generic_mmap_write_config(bus, mtk_pcie_config_address,
97 bdf, offset, value, size);
98}
99
100static const struct dm_pci_ops mtk_pcie_ops = {
101 .read_config = mtk_pcie_read_config,
102 .write_config = mtk_pcie_write_config,
103};
104
105static void mtk_pcie_port_free(struct mtk_pcie_port *port)
106{
107 list_del(&port->list);
108 free(port);
109}
110
111static int mtk_pcie_startup_port(struct mtk_pcie_port *port)
112{
113 struct mtk_pcie *pcie = port->pcie;
114 u32 slot = PCI_DEV(port->slot << 11);
115 u32 val;
116 int err;
117
118 /* assert port PERST_N */
119 setbits_le32(pcie->base + PCIE_SYS_CFG, PCIE_PORT_PERST(port->slot));
120 /* de-assert port PERST_N */
121 clrbits_le32(pcie->base + PCIE_SYS_CFG, PCIE_PORT_PERST(port->slot));
122
123 /* 100ms timeout value should be enough for Gen1/2 training */
124 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val,
125 !!(val & PCIE_PORT_LINKUP), 100000);
126 if (err)
127 return -ETIMEDOUT;
128
129 /* disable interrupt */
130 clrbits_le32(pcie->base + PCIE_INT_ENABLE,
131 PCIE_PORT_INT_EN(port->slot));
132
133 /* map to all DDR region. We need to set it before cfg operation. */
134 writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
135 port->base + PCIE_BAR0_SETUP);
136
137 /* configure class code and revision ID */
138 writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS);
139
140 /* configure FC credit */
141 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, slot),
142 pcie->base + PCIE_CFG_ADDR);
143 clrsetbits_le32(pcie->base + PCIE_CFG_DATA, PCIE_FC_CREDIT_MASK,
144 PCIE_FC_CREDIT_VAL(0x806c));
145
146 /* configure RC FTS number to 250 when it leaves L0s */
147 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, slot), pcie->base + PCIE_CFG_ADDR);
148 clrsetbits_le32(pcie->base + PCIE_CFG_DATA, PCIE_FTS_NUM_MASK,
149 PCIE_FTS_NUM_L0(0x50));
150
151 return 0;
152}
153
154static void mtk_pcie_enable_port(struct mtk_pcie_port *port)
155{
156 int err;
157
158 err = clk_enable(&port->sys_ck);
159 if (err)
160 goto exit;
161
162 err = reset_assert(&port->reset);
163 if (err)
164 goto exit;
165
166 err = reset_deassert(&port->reset);
167 if (err)
168 goto exit;
169
170 err = generic_phy_init(&port->phy);
171 if (err)
172 goto exit;
173
174 err = generic_phy_power_on(&port->phy);
175 if (err)
176 goto exit;
177
178 if (!mtk_pcie_startup_port(port))
179 return;
180
181 pr_err("Port%d link down\n", port->slot);
182exit:
183 mtk_pcie_port_free(port);
184}
185
186static int mtk_pcie_parse_port(struct udevice *dev, u32 slot)
187{
188 struct mtk_pcie *pcie = dev_get_priv(dev);
189 struct mtk_pcie_port *port;
190 char name[10];
191 int err;
192
193 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
194 if (!port)
195 return -ENOMEM;
196
197 snprintf(name, sizeof(name), "port%d", slot);
198 port->base = dev_remap_addr_name(dev, name);
199 if (!port->base)
200 return -ENOENT;
201
202 snprintf(name, sizeof(name), "sys_ck%d", slot);
203 err = clk_get_by_name(dev, name, &port->sys_ck);
204 if (err)
205 return err;
206
207 err = reset_get_by_index(dev, slot, &port->reset);
208 if (err)
209 return err;
210
211 err = generic_phy_get_by_index(dev, slot, &port->phy);
212 if (err)
213 return err;
214
215 port->slot = slot;
216 port->pcie = pcie;
217
218 INIT_LIST_HEAD(&port->list);
219 list_add_tail(&port->list, &pcie->ports);
220
221 return 0;
222}
223
224static int mtk_pcie_probe(struct udevice *dev)
225{
226 struct mtk_pcie *pcie = dev_get_priv(dev);
227 struct mtk_pcie_port *port, *tmp;
228 ofnode subnode;
229 int err;
230
231 INIT_LIST_HEAD(&pcie->ports);
232
233 pcie->base = dev_remap_addr_name(dev, "subsys");
234 if (!pcie->base)
235 return -ENOENT;
236
237 err = clk_get_by_name(dev, "free_ck", &pcie->free_ck);
238 if (err)
239 return err;
240
241 /* enable top level clock */
242 err = clk_enable(&pcie->free_ck);
243 if (err)
244 return err;
245
246 dev_for_each_subnode(subnode, dev) {
247 struct fdt_pci_addr addr;
248 u32 slot = 0;
249
250 if (!ofnode_is_available(subnode))
251 continue;
252
253 err = ofnode_read_pci_addr(subnode, 0, "reg", &addr);
254 if (err)
255 return err;
256
257 slot = PCI_DEV(addr.phys_hi);
258
259 err = mtk_pcie_parse_port(dev, slot);
260 if (err)
261 return err;
262 }
263
264 /* enable each port, and then check link status */
265 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
266 mtk_pcie_enable_port(port);
267
268 return 0;
269}
270
271static const struct udevice_id mtk_pcie_ids[] = {
272 { .compatible = "mediatek,mt7623-pcie", },
273 { }
274};
275
276U_BOOT_DRIVER(pcie_mediatek) = {
277 .name = "pcie_mediatek",
278 .id = UCLASS_PCI,
279 .of_match = mtk_pcie_ids,
280 .ops = &mtk_pcie_ops,
281 .probe = mtk_pcie_probe,
282 .priv_auto_alloc_size = sizeof(struct mtk_pcie),
283};