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