xref: /linux/drivers/pci/controller/dwc/pci-meson.c (revision 43dfc13ca972988e620a6edb72956981b75ab6b0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe host controller driver for Amlogic MESON SoCs
4  *
5  * Copyright (c) 2018 Amlogic, inc.
6  * Author: Yue Wang <yue.wang@amlogic.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/pci.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset.h>
15 #include <linux/resource.h>
16 #include <linux/types.h>
17 #include <linux/phy/phy.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
20 
21 #include "pcie-designware.h"
22 
23 #define to_meson_pcie(x) dev_get_drvdata((x)->dev)
24 
25 #define PCIE_CAP_MAX_PAYLOAD_SIZE(x)	((x) << 5)
26 #define PCIE_CAP_MAX_READ_REQ_SIZE(x)	((x) << 12)
27 
28 /* PCIe specific config registers */
29 #define PCIE_CFG0			0x0
30 #define APP_LTSSM_ENABLE		BIT(7)
31 
32 #define PCIE_CFG_STATUS12		0x30
33 #define IS_SMLH_LINK_UP(x)		((x) & (1 << 6))
34 #define IS_RDLH_LINK_UP(x)		((x) & (1 << 16))
35 #define IS_LTSSM_UP(x)			((((x) >> 10) & 0x1f) == 0x11)
36 
37 #define PCIE_CFG_STATUS17		0x44
38 #define PM_CURRENT_STATE(x)		(((x) >> 7) & 0x1)
39 
40 #define WAIT_LINKUP_TIMEOUT		4000
41 #define PORT_CLK_RATE			100000000UL
42 #define MAX_PAYLOAD_SIZE		256
43 #define MAX_READ_REQ_SIZE		256
44 #define PCIE_RESET_DELAY		500
45 #define PCIE_SHARED_RESET		1
46 #define PCIE_NORMAL_RESET		0
47 
48 enum pcie_data_rate {
49 	PCIE_GEN1,
50 	PCIE_GEN2,
51 	PCIE_GEN3,
52 	PCIE_GEN4
53 };
54 
55 struct meson_pcie_clk_res {
56 	struct clk *clk;
57 	struct clk *port_clk;
58 	struct clk *general_clk;
59 };
60 
61 struct meson_pcie_rc_reset {
62 	struct reset_control *port;
63 	struct reset_control *apb;
64 };
65 
66 struct meson_pcie {
67 	struct dw_pcie pci;
68 	void __iomem *cfg_base;
69 	struct meson_pcie_clk_res clk_res;
70 	struct meson_pcie_rc_reset mrst;
71 	struct gpio_desc *reset_gpio;
72 	struct phy *phy;
73 };
74 
75 static struct reset_control *meson_pcie_get_reset(struct meson_pcie *mp,
76 						  const char *id,
77 						  u32 reset_type)
78 {
79 	struct device *dev = mp->pci.dev;
80 	struct reset_control *reset;
81 
82 	if (reset_type == PCIE_SHARED_RESET)
83 		reset = devm_reset_control_get_shared(dev, id);
84 	else
85 		reset = devm_reset_control_get(dev, id);
86 
87 	return reset;
88 }
89 
90 static int meson_pcie_get_resets(struct meson_pcie *mp)
91 {
92 	struct meson_pcie_rc_reset *mrst = &mp->mrst;
93 
94 	mrst->port = meson_pcie_get_reset(mp, "port", PCIE_NORMAL_RESET);
95 	if (IS_ERR(mrst->port))
96 		return PTR_ERR(mrst->port);
97 	reset_control_deassert(mrst->port);
98 
99 	mrst->apb = meson_pcie_get_reset(mp, "apb", PCIE_SHARED_RESET);
100 	if (IS_ERR(mrst->apb))
101 		return PTR_ERR(mrst->apb);
102 	reset_control_deassert(mrst->apb);
103 
104 	return 0;
105 }
106 
107 static int meson_pcie_get_mems(struct platform_device *pdev,
108 			       struct meson_pcie *mp)
109 {
110 	struct dw_pcie *pci = &mp->pci;
111 	struct resource *res;
112 
113 	/*
114 	 * For the broken DTs that supply 'dbi' as 'elbi', parse the 'elbi'
115 	 * region and assign it to both 'pci->elbi_base' and 'pci->dbi_space' so
116 	 * that the DWC core can skip parsing both regions.
117 	 */
118 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "elbi");
119 	if (res) {
120 		pci->elbi_base = devm_pci_remap_cfg_resource(pci->dev, res);
121 		if (IS_ERR(pci->elbi_base))
122 			return PTR_ERR(pci->elbi_base);
123 
124 		pci->dbi_base = pci->elbi_base;
125 		pci->dbi_phys_addr = res->start;
126 	}
127 
128 	mp->cfg_base = devm_platform_ioremap_resource_byname(pdev, "cfg");
129 	if (IS_ERR(mp->cfg_base))
130 		return PTR_ERR(mp->cfg_base);
131 
132 	return 0;
133 }
134 
135 static int meson_pcie_power_on(struct meson_pcie *mp)
136 {
137 	int ret = 0;
138 
139 	ret = phy_init(mp->phy);
140 	if (ret)
141 		return ret;
142 
143 	ret = phy_power_on(mp->phy);
144 	if (ret) {
145 		phy_exit(mp->phy);
146 		return ret;
147 	}
148 
149 	return 0;
150 }
151 
152 static void meson_pcie_power_off(struct meson_pcie *mp)
153 {
154 	phy_power_off(mp->phy);
155 	phy_exit(mp->phy);
156 }
157 
158 static int meson_pcie_reset(struct meson_pcie *mp)
159 {
160 	struct meson_pcie_rc_reset *mrst = &mp->mrst;
161 	int ret = 0;
162 
163 	ret = phy_reset(mp->phy);
164 	if (ret)
165 		return ret;
166 
167 	reset_control_assert(mrst->port);
168 	reset_control_assert(mrst->apb);
169 	udelay(PCIE_RESET_DELAY);
170 	reset_control_deassert(mrst->port);
171 	reset_control_deassert(mrst->apb);
172 	udelay(PCIE_RESET_DELAY);
173 
174 	return 0;
175 }
176 
177 static inline void meson_pcie_disable_clock(void *data)
178 {
179 	struct clk *clk = data;
180 
181 	clk_disable_unprepare(clk);
182 }
183 
184 static inline struct clk *meson_pcie_probe_clock(struct device *dev,
185 						 const char *id, u64 rate)
186 {
187 	struct clk *clk;
188 	int ret;
189 
190 	clk = devm_clk_get(dev, id);
191 	if (IS_ERR(clk))
192 		return clk;
193 
194 	if (rate) {
195 		ret = clk_set_rate(clk, rate);
196 		if (ret) {
197 			dev_err(dev, "set clk rate failed, ret = %d\n", ret);
198 			return ERR_PTR(ret);
199 		}
200 	}
201 
202 	ret = clk_prepare_enable(clk);
203 	if (ret) {
204 		dev_err(dev, "couldn't enable clk\n");
205 		return ERR_PTR(ret);
206 	}
207 
208 	devm_add_action_or_reset(dev, meson_pcie_disable_clock, clk);
209 
210 	return clk;
211 }
212 
213 static int meson_pcie_probe_clocks(struct meson_pcie *mp)
214 {
215 	struct device *dev = mp->pci.dev;
216 	struct meson_pcie_clk_res *res = &mp->clk_res;
217 
218 	res->port_clk = meson_pcie_probe_clock(dev, "port", PORT_CLK_RATE);
219 	if (IS_ERR(res->port_clk))
220 		return PTR_ERR(res->port_clk);
221 
222 	res->general_clk = meson_pcie_probe_clock(dev, "general", 0);
223 	if (IS_ERR(res->general_clk))
224 		return PTR_ERR(res->general_clk);
225 
226 	res->clk = meson_pcie_probe_clock(dev, "pclk", 0);
227 	if (IS_ERR(res->clk))
228 		return PTR_ERR(res->clk);
229 
230 	return 0;
231 }
232 
233 static inline u32 meson_cfg_readl(struct meson_pcie *mp, u32 reg)
234 {
235 	return readl(mp->cfg_base + reg);
236 }
237 
238 static inline void meson_cfg_writel(struct meson_pcie *mp, u32 val, u32 reg)
239 {
240 	writel(val, mp->cfg_base + reg);
241 }
242 
243 static void meson_pcie_assert_reset(struct meson_pcie *mp)
244 {
245 	gpiod_set_value_cansleep(mp->reset_gpio, 1);
246 	udelay(500);
247 	gpiod_set_value_cansleep(mp->reset_gpio, 0);
248 }
249 
250 static void meson_pcie_ltssm_enable(struct meson_pcie *mp)
251 {
252 	u32 val;
253 
254 	val = meson_cfg_readl(mp, PCIE_CFG0);
255 	val |= APP_LTSSM_ENABLE;
256 	meson_cfg_writel(mp, val, PCIE_CFG0);
257 }
258 
259 static int meson_size_to_payload(struct meson_pcie *mp, int size)
260 {
261 	struct device *dev = mp->pci.dev;
262 
263 	/*
264 	 * dwc supports 2^(val+7) payload size, which val is 0~5 default to 1.
265 	 * So if input size is not 2^order alignment or less than 2^7 or bigger
266 	 * than 2^12, just set to default size 2^(1+7).
267 	 */
268 	if (!is_power_of_2(size) || size < 128 || size > 4096) {
269 		dev_warn(dev, "payload size %d, set to default 256\n", size);
270 		return 1;
271 	}
272 
273 	return fls(size) - 8;
274 }
275 
276 static void meson_set_max_payload(struct meson_pcie *mp, int size)
277 {
278 	struct dw_pcie *pci = &mp->pci;
279 	u32 val;
280 	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
281 	int max_payload_size = meson_size_to_payload(mp, size);
282 
283 	val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
284 	val &= ~PCI_EXP_DEVCTL_PAYLOAD;
285 	dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
286 
287 	val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
288 	val |= PCIE_CAP_MAX_PAYLOAD_SIZE(max_payload_size);
289 	dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
290 }
291 
292 static void meson_set_max_rd_req_size(struct meson_pcie *mp, int size)
293 {
294 	struct dw_pcie *pci = &mp->pci;
295 	u32 val;
296 	u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
297 	int max_rd_req_size = meson_size_to_payload(mp, size);
298 
299 	val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
300 	val &= ~PCI_EXP_DEVCTL_READRQ;
301 	dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
302 
303 	val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
304 	val |= PCIE_CAP_MAX_READ_REQ_SIZE(max_rd_req_size);
305 	dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
306 }
307 
308 static int meson_pcie_start_link(struct dw_pcie *pci)
309 {
310 	struct meson_pcie *mp = to_meson_pcie(pci);
311 
312 	meson_pcie_ltssm_enable(mp);
313 	meson_pcie_assert_reset(mp);
314 
315 	return 0;
316 }
317 
318 static int meson_pcie_rd_own_conf(struct pci_bus *bus, u32 devfn,
319 				  int where, int size, u32 *val)
320 {
321 	int ret;
322 
323 	ret = pci_generic_config_read(bus, devfn, where, size, val);
324 	if (ret != PCIBIOS_SUCCESSFUL)
325 		return ret;
326 
327 	/*
328 	 * There is a bug in the MESON AXG PCIe controller whereby software
329 	 * cannot program the PCI_CLASS_DEVICE register, so we must fabricate
330 	 * the return value in the config accessors.
331 	 */
332 	if ((where & ~3) == PCI_CLASS_REVISION) {
333 		if (size <= 2)
334 			*val = (*val & ((1 << (size * 8)) - 1)) << (8 * (where & 3));
335 		*val &= ~0xffffff00;
336 		*val |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
337 		if (size <= 2)
338 			*val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
339 	}
340 
341 	return PCIBIOS_SUCCESSFUL;
342 }
343 
344 static struct pci_ops meson_pci_ops = {
345 	.map_bus = dw_pcie_own_conf_map_bus,
346 	.read = meson_pcie_rd_own_conf,
347 	.write = pci_generic_config_write,
348 };
349 
350 static bool meson_pcie_link_up(struct dw_pcie *pci)
351 {
352 	struct meson_pcie *mp = to_meson_pcie(pci);
353 	struct device *dev = pci->dev;
354 	u32 speed_okay = 0;
355 	u32 cnt = 0;
356 	u32 state12, state17, smlh_up, ltssm_up, rdlh_up;
357 
358 	do {
359 		state12 = meson_cfg_readl(mp, PCIE_CFG_STATUS12);
360 		state17 = meson_cfg_readl(mp, PCIE_CFG_STATUS17);
361 		smlh_up = IS_SMLH_LINK_UP(state12);
362 		rdlh_up = IS_RDLH_LINK_UP(state12);
363 		ltssm_up = IS_LTSSM_UP(state12);
364 
365 		if (PM_CURRENT_STATE(state17) < PCIE_GEN3)
366 			speed_okay = 1;
367 
368 		if (smlh_up)
369 			dev_dbg(dev, "smlh_link_up is on\n");
370 		if (rdlh_up)
371 			dev_dbg(dev, "rdlh_link_up is on\n");
372 		if (ltssm_up)
373 			dev_dbg(dev, "ltssm_up is on\n");
374 		if (speed_okay)
375 			dev_dbg(dev, "speed_okay\n");
376 
377 		if (smlh_up && rdlh_up && ltssm_up && speed_okay)
378 			return true;
379 
380 		cnt++;
381 
382 		udelay(10);
383 	} while (cnt < WAIT_LINKUP_TIMEOUT);
384 
385 	dev_err(dev, "error: wait linkup timeout\n");
386 	return false;
387 }
388 
389 static int meson_pcie_host_init(struct dw_pcie_rp *pp)
390 {
391 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
392 	struct meson_pcie *mp = to_meson_pcie(pci);
393 
394 	pp->bridge->ops = &meson_pci_ops;
395 
396 	meson_set_max_payload(mp, MAX_PAYLOAD_SIZE);
397 	meson_set_max_rd_req_size(mp, MAX_READ_REQ_SIZE);
398 
399 	return 0;
400 }
401 
402 static const struct dw_pcie_host_ops meson_pcie_host_ops = {
403 	.init = meson_pcie_host_init,
404 };
405 
406 static const struct dw_pcie_ops dw_pcie_ops = {
407 	.link_up = meson_pcie_link_up,
408 	.start_link = meson_pcie_start_link,
409 };
410 
411 static int meson_pcie_probe(struct platform_device *pdev)
412 {
413 	struct device *dev = &pdev->dev;
414 	struct dw_pcie *pci;
415 	struct meson_pcie *mp;
416 	int ret;
417 
418 	mp = devm_kzalloc(dev, sizeof(*mp), GFP_KERNEL);
419 	if (!mp)
420 		return -ENOMEM;
421 
422 	pci = &mp->pci;
423 	pci->dev = dev;
424 	pci->ops = &dw_pcie_ops;
425 	pci->pp.ops = &meson_pcie_host_ops;
426 	pci->num_lanes = 1;
427 
428 	mp->phy = devm_phy_get(dev, "pcie");
429 	if (IS_ERR(mp->phy)) {
430 		dev_err(dev, "get phy failed, %ld\n", PTR_ERR(mp->phy));
431 		return PTR_ERR(mp->phy);
432 	}
433 
434 	mp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
435 	if (IS_ERR(mp->reset_gpio)) {
436 		dev_err(dev, "get reset gpio failed\n");
437 		return PTR_ERR(mp->reset_gpio);
438 	}
439 
440 	ret = meson_pcie_get_resets(mp);
441 	if (ret) {
442 		dev_err(dev, "get reset resource failed, %d\n", ret);
443 		return ret;
444 	}
445 
446 	ret = meson_pcie_get_mems(pdev, mp);
447 	if (ret) {
448 		dev_err(dev, "get memory resource failed, %d\n", ret);
449 		return ret;
450 	}
451 
452 	ret = meson_pcie_power_on(mp);
453 	if (ret) {
454 		dev_err(dev, "phy power on failed, %d\n", ret);
455 		return ret;
456 	}
457 
458 	ret = meson_pcie_reset(mp);
459 	if (ret) {
460 		dev_err(dev, "reset failed, %d\n", ret);
461 		goto err_phy;
462 	}
463 
464 	ret = meson_pcie_probe_clocks(mp);
465 	if (ret) {
466 		dev_err(dev, "init clock resources failed, %d\n", ret);
467 		goto err_phy;
468 	}
469 
470 	platform_set_drvdata(pdev, mp);
471 
472 	ret = dw_pcie_host_init(&pci->pp);
473 	if (ret < 0) {
474 		dev_err(dev, "Add PCIe port failed, %d\n", ret);
475 		goto err_phy;
476 	}
477 
478 	return 0;
479 
480 err_phy:
481 	meson_pcie_power_off(mp);
482 	return ret;
483 }
484 
485 static const struct of_device_id meson_pcie_of_match[] = {
486 	{
487 		.compatible = "amlogic,axg-pcie",
488 	},
489 	{
490 		.compatible = "amlogic,g12a-pcie",
491 	},
492 	{},
493 };
494 MODULE_DEVICE_TABLE(of, meson_pcie_of_match);
495 
496 static struct platform_driver meson_pcie_driver = {
497 	.probe = meson_pcie_probe,
498 	.driver = {
499 		.name = "meson-pcie",
500 		.of_match_table = meson_pcie_of_match,
501 	},
502 };
503 
504 module_platform_driver(meson_pcie_driver);
505 
506 MODULE_AUTHOR("Yue Wang <yue.wang@amlogic.com>");
507 MODULE_DESCRIPTION("Amlogic PCIe Controller driver");
508 MODULE_LICENSE("GPL v2");
509