Linux Audio

Check our new training course

Loading...
Note: File does not exist in v5.9.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * PCIe controller driver for Renesas R-Car Gen4 Series SoCs
  4 * Copyright (C) 2022-2023 Renesas Electronics Corporation
  5 */
  6
  7#include <linux/delay.h>
  8#include <linux/interrupt.h>
  9#include <linux/io.h>
 10#include <linux/module.h>
 11#include <linux/of.h>
 12#include <linux/pci.h>
 13#include <linux/platform_device.h>
 14#include <linux/pm_runtime.h>
 15#include <linux/reset.h>
 16
 17#include "../../pci.h"
 18#include "pcie-designware.h"
 19
 20/* Renesas-specific */
 21/* PCIe Mode Setting Register 0 */
 22#define PCIEMSR0		0x0000
 23#define BIFUR_MOD_SET_ON	BIT(0)
 24#define DEVICE_TYPE_EP		0
 25#define DEVICE_TYPE_RC		BIT(4)
 26
 27/* PCIe Interrupt Status 0 */
 28#define PCIEINTSTS0		0x0084
 29
 30/* PCIe Interrupt Status 0 Enable */
 31#define PCIEINTSTS0EN		0x0310
 32#define MSI_CTRL_INT		BIT(26)
 33#define SMLH_LINK_UP		BIT(7)
 34#define RDLH_LINK_UP		BIT(6)
 35
 36/* PCIe DMA Interrupt Status Enable */
 37#define PCIEDMAINTSTSEN		0x0314
 38#define PCIEDMAINTSTSEN_INIT	GENMASK(15, 0)
 39
 40/* PCIe Reset Control Register 1 */
 41#define PCIERSTCTRL1		0x0014
 42#define APP_HOLD_PHY_RST	BIT(16)
 43#define APP_LTSSM_ENABLE	BIT(0)
 44
 45#define RCAR_NUM_SPEED_CHANGE_RETRIES	10
 46#define RCAR_MAX_LINK_SPEED		4
 47
 48#define RCAR_GEN4_PCIE_EP_FUNC_DBI_OFFSET	0x1000
 49#define RCAR_GEN4_PCIE_EP_FUNC_DBI2_OFFSET	0x800
 50
 51struct rcar_gen4_pcie {
 52	struct dw_pcie dw;
 53	void __iomem *base;
 54	struct platform_device *pdev;
 55	enum dw_pcie_device_mode mode;
 56};
 57#define to_rcar_gen4_pcie(_dw)	container_of(_dw, struct rcar_gen4_pcie, dw)
 58
 59/* Common */
 60static void rcar_gen4_pcie_ltssm_enable(struct rcar_gen4_pcie *rcar,
 61					bool enable)
 62{
 63	u32 val;
 64
 65	val = readl(rcar->base + PCIERSTCTRL1);
 66	if (enable) {
 67		val |= APP_LTSSM_ENABLE;
 68		val &= ~APP_HOLD_PHY_RST;
 69	} else {
 70		/*
 71		 * Since the datasheet of R-Car doesn't mention how to assert
 72		 * the APP_HOLD_PHY_RST, don't assert it again. Otherwise,
 73		 * hang-up issue happened in the dw_edma_core_off() when
 74		 * the controller didn't detect a PCI device.
 75		 */
 76		val &= ~APP_LTSSM_ENABLE;
 77	}
 78	writel(val, rcar->base + PCIERSTCTRL1);
 79}
 80
 81static int rcar_gen4_pcie_link_up(struct dw_pcie *dw)
 82{
 83	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
 84	u32 val, mask;
 85
 86	val = readl(rcar->base + PCIEINTSTS0);
 87	mask = RDLH_LINK_UP | SMLH_LINK_UP;
 88
 89	return (val & mask) == mask;
 90}
 91
 92/*
 93 * Manually initiate the speed change. Return 0 if change succeeded; otherwise
 94 * -ETIMEDOUT.
 95 */
 96static int rcar_gen4_pcie_speed_change(struct dw_pcie *dw)
 97{
 98	u32 val;
 99	int i;
100
101	val = dw_pcie_readl_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL);
102	val &= ~PORT_LOGIC_SPEED_CHANGE;
103	dw_pcie_writel_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
104
105	val = dw_pcie_readl_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL);
106	val |= PORT_LOGIC_SPEED_CHANGE;
107	dw_pcie_writel_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
108
109	for (i = 0; i < RCAR_NUM_SPEED_CHANGE_RETRIES; i++) {
110		val = dw_pcie_readl_dbi(dw, PCIE_LINK_WIDTH_SPEED_CONTROL);
111		if (!(val & PORT_LOGIC_SPEED_CHANGE))
112			return 0;
113		usleep_range(10000, 11000);
114	}
115
116	return -ETIMEDOUT;
117}
118
119/*
120 * Enable LTSSM of this controller and manually initiate the speed change.
121 * Always return 0.
122 */
123static int rcar_gen4_pcie_start_link(struct dw_pcie *dw)
124{
125	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
126	int i, changes;
127
128	rcar_gen4_pcie_ltssm_enable(rcar, true);
129
130	/*
131	 * Require direct speed change with retrying here if the link_gen is
132	 * PCIe Gen2 or higher.
133	 */
134	changes = min_not_zero(dw->link_gen, RCAR_MAX_LINK_SPEED) - 1;
135
136	/*
137	 * Since dw_pcie_setup_rc() sets it once, PCIe Gen2 will be trained.
138	 * So, this needs remaining times for up to PCIe Gen4 if RC mode.
139	 */
140	if (changes && rcar->mode == DW_PCIE_RC_TYPE)
141		changes--;
142
143	for (i = 0; i < changes; i++) {
144		/* It may not be connected in EP mode yet. So, break the loop */
145		if (rcar_gen4_pcie_speed_change(dw))
146			break;
147	}
148
149	return 0;
150}
151
152static void rcar_gen4_pcie_stop_link(struct dw_pcie *dw)
153{
154	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
155
156	rcar_gen4_pcie_ltssm_enable(rcar, false);
157}
158
159static int rcar_gen4_pcie_common_init(struct rcar_gen4_pcie *rcar)
160{
161	struct dw_pcie *dw = &rcar->dw;
162	u32 val;
163	int ret;
164
165	ret = clk_bulk_prepare_enable(DW_PCIE_NUM_CORE_CLKS, dw->core_clks);
166	if (ret) {
167		dev_err(dw->dev, "Enabling core clocks failed\n");
168		return ret;
169	}
170
171	if (!reset_control_status(dw->core_rsts[DW_PCIE_PWR_RST].rstc))
172		reset_control_assert(dw->core_rsts[DW_PCIE_PWR_RST].rstc);
173
174	val = readl(rcar->base + PCIEMSR0);
175	if (rcar->mode == DW_PCIE_RC_TYPE) {
176		val |= DEVICE_TYPE_RC;
177	} else if (rcar->mode == DW_PCIE_EP_TYPE) {
178		val |= DEVICE_TYPE_EP;
179	} else {
180		ret = -EINVAL;
181		goto err_unprepare;
182	}
183
184	if (dw->num_lanes < 4)
185		val |= BIFUR_MOD_SET_ON;
186
187	writel(val, rcar->base + PCIEMSR0);
188
189	ret = reset_control_deassert(dw->core_rsts[DW_PCIE_PWR_RST].rstc);
190	if (ret)
191		goto err_unprepare;
192
193	return 0;
194
195err_unprepare:
196	clk_bulk_disable_unprepare(DW_PCIE_NUM_CORE_CLKS, dw->core_clks);
197
198	return ret;
199}
200
201static void rcar_gen4_pcie_common_deinit(struct rcar_gen4_pcie *rcar)
202{
203	struct dw_pcie *dw = &rcar->dw;
204
205	reset_control_assert(dw->core_rsts[DW_PCIE_PWR_RST].rstc);
206	clk_bulk_disable_unprepare(DW_PCIE_NUM_CORE_CLKS, dw->core_clks);
207}
208
209static int rcar_gen4_pcie_prepare(struct rcar_gen4_pcie *rcar)
210{
211	struct device *dev = rcar->dw.dev;
212	int err;
213
214	pm_runtime_enable(dev);
215	err = pm_runtime_resume_and_get(dev);
216	if (err < 0) {
217		dev_err(dev, "Runtime resume failed\n");
218		pm_runtime_disable(dev);
219	}
220
221	return err;
222}
223
224static void rcar_gen4_pcie_unprepare(struct rcar_gen4_pcie *rcar)
225{
226	struct device *dev = rcar->dw.dev;
227
228	pm_runtime_put(dev);
229	pm_runtime_disable(dev);
230}
231
232static int rcar_gen4_pcie_get_resources(struct rcar_gen4_pcie *rcar)
233{
234	/* Renesas-specific registers */
235	rcar->base = devm_platform_ioremap_resource_byname(rcar->pdev, "app");
236
237	return PTR_ERR_OR_ZERO(rcar->base);
238}
239
240static const struct dw_pcie_ops dw_pcie_ops = {
241	.start_link = rcar_gen4_pcie_start_link,
242	.stop_link = rcar_gen4_pcie_stop_link,
243	.link_up = rcar_gen4_pcie_link_up,
244};
245
246static struct rcar_gen4_pcie *rcar_gen4_pcie_alloc(struct platform_device *pdev)
247{
248	struct device *dev = &pdev->dev;
249	struct rcar_gen4_pcie *rcar;
250
251	rcar = devm_kzalloc(dev, sizeof(*rcar), GFP_KERNEL);
252	if (!rcar)
253		return ERR_PTR(-ENOMEM);
254
255	rcar->dw.ops = &dw_pcie_ops;
256	rcar->dw.dev = dev;
257	rcar->pdev = pdev;
258	dw_pcie_cap_set(&rcar->dw, EDMA_UNROLL);
259	dw_pcie_cap_set(&rcar->dw, REQ_RES);
260	platform_set_drvdata(pdev, rcar);
261
262	return rcar;
263}
264
265/* Host mode */
266static int rcar_gen4_pcie_host_init(struct dw_pcie_rp *pp)
267{
268	struct dw_pcie *dw = to_dw_pcie_from_pp(pp);
269	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
270	int ret;
271	u32 val;
272
273	gpiod_set_value_cansleep(dw->pe_rst, 1);
274
275	ret = rcar_gen4_pcie_common_init(rcar);
276	if (ret)
277		return ret;
278
279	/*
280	 * According to the section 3.5.7.2 "RC Mode" in DWC PCIe Dual Mode
281	 * Rev.5.20a and 3.5.6.1 "RC mode" in DWC PCIe RC databook v5.20a, we
282	 * should disable two BARs to avoid unnecessary memory assignment
283	 * during device enumeration.
284	 */
285	dw_pcie_writel_dbi2(dw, PCI_BASE_ADDRESS_0, 0x0);
286	dw_pcie_writel_dbi2(dw, PCI_BASE_ADDRESS_1, 0x0);
287
288	/* Enable MSI interrupt signal */
289	val = readl(rcar->base + PCIEINTSTS0EN);
290	val |= MSI_CTRL_INT;
291	writel(val, rcar->base + PCIEINTSTS0EN);
292
293	msleep(PCIE_T_PVPERL_MS);	/* pe_rst requires 100msec delay */
294
295	gpiod_set_value_cansleep(dw->pe_rst, 0);
296
297	return 0;
298}
299
300static void rcar_gen4_pcie_host_deinit(struct dw_pcie_rp *pp)
301{
302	struct dw_pcie *dw = to_dw_pcie_from_pp(pp);
303	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
304
305	gpiod_set_value_cansleep(dw->pe_rst, 1);
306	rcar_gen4_pcie_common_deinit(rcar);
307}
308
309static const struct dw_pcie_host_ops rcar_gen4_pcie_host_ops = {
310	.init = rcar_gen4_pcie_host_init,
311	.deinit = rcar_gen4_pcie_host_deinit,
312};
313
314static int rcar_gen4_add_dw_pcie_rp(struct rcar_gen4_pcie *rcar)
315{
316	struct dw_pcie_rp *pp = &rcar->dw.pp;
317
318	if (!IS_ENABLED(CONFIG_PCIE_RCAR_GEN4_HOST))
319		return -ENODEV;
320
321	pp->num_vectors = MAX_MSI_IRQS;
322	pp->ops = &rcar_gen4_pcie_host_ops;
323
324	return dw_pcie_host_init(pp);
325}
326
327static void rcar_gen4_remove_dw_pcie_rp(struct rcar_gen4_pcie *rcar)
328{
329	dw_pcie_host_deinit(&rcar->dw.pp);
330}
331
332/* Endpoint mode */
333static void rcar_gen4_pcie_ep_pre_init(struct dw_pcie_ep *ep)
334{
335	struct dw_pcie *dw = to_dw_pcie_from_ep(ep);
336	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
337	int ret;
338
339	ret = rcar_gen4_pcie_common_init(rcar);
340	if (ret)
341		return;
342
343	writel(PCIEDMAINTSTSEN_INIT, rcar->base + PCIEDMAINTSTSEN);
344}
345
346static void rcar_gen4_pcie_ep_init(struct dw_pcie_ep *ep)
347{
348	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
349	enum pci_barno bar;
350
351	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
352		dw_pcie_ep_reset_bar(pci, bar);
353}
354
355static void rcar_gen4_pcie_ep_deinit(struct dw_pcie_ep *ep)
356{
357	struct dw_pcie *dw = to_dw_pcie_from_ep(ep);
358	struct rcar_gen4_pcie *rcar = to_rcar_gen4_pcie(dw);
359
360	writel(0, rcar->base + PCIEDMAINTSTSEN);
361	rcar_gen4_pcie_common_deinit(rcar);
362}
363
364static int rcar_gen4_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
365				       unsigned int type, u16 interrupt_num)
366{
367	struct dw_pcie *dw = to_dw_pcie_from_ep(ep);
368
369	switch (type) {
370	case PCI_IRQ_INTX:
371		return dw_pcie_ep_raise_intx_irq(ep, func_no);
372	case PCI_IRQ_MSI:
373		return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
374	default:
375		dev_err(dw->dev, "Unknown IRQ type\n");
376		return -EINVAL;
377	}
378
379	return 0;
380}
381
382static const struct pci_epc_features rcar_gen4_pcie_epc_features = {
383	.linkup_notifier = false,
384	.msi_capable = true,
385	.msix_capable = false,
386	.reserved_bar = 1 << BAR_1 | 1 << BAR_3 | 1 << BAR_5,
387	.align = SZ_1M,
388};
389
390static const struct pci_epc_features*
391rcar_gen4_pcie_ep_get_features(struct dw_pcie_ep *ep)
392{
393	return &rcar_gen4_pcie_epc_features;
394}
395
396static unsigned int rcar_gen4_pcie_ep_get_dbi_offset(struct dw_pcie_ep *ep,
397						       u8 func_no)
398{
399	return func_no * RCAR_GEN4_PCIE_EP_FUNC_DBI_OFFSET;
400}
401
402static unsigned int rcar_gen4_pcie_ep_get_dbi2_offset(struct dw_pcie_ep *ep,
403						      u8 func_no)
404{
405	return func_no * RCAR_GEN4_PCIE_EP_FUNC_DBI2_OFFSET;
406}
407
408static const struct dw_pcie_ep_ops pcie_ep_ops = {
409	.pre_init = rcar_gen4_pcie_ep_pre_init,
410	.init = rcar_gen4_pcie_ep_init,
411	.deinit = rcar_gen4_pcie_ep_deinit,
412	.raise_irq = rcar_gen4_pcie_ep_raise_irq,
413	.get_features = rcar_gen4_pcie_ep_get_features,
414	.get_dbi_offset = rcar_gen4_pcie_ep_get_dbi_offset,
415	.get_dbi2_offset = rcar_gen4_pcie_ep_get_dbi2_offset,
416};
417
418static int rcar_gen4_add_dw_pcie_ep(struct rcar_gen4_pcie *rcar)
419{
420	struct dw_pcie_ep *ep = &rcar->dw.ep;
421
422	if (!IS_ENABLED(CONFIG_PCIE_RCAR_GEN4_EP))
423		return -ENODEV;
424
425	ep->ops = &pcie_ep_ops;
426
427	return dw_pcie_ep_init(ep);
428}
429
430static void rcar_gen4_remove_dw_pcie_ep(struct rcar_gen4_pcie *rcar)
431{
432	dw_pcie_ep_exit(&rcar->dw.ep);
433}
434
435/* Common */
436static int rcar_gen4_add_dw_pcie(struct rcar_gen4_pcie *rcar)
437{
438	rcar->mode = (uintptr_t)of_device_get_match_data(&rcar->pdev->dev);
439
440	switch (rcar->mode) {
441	case DW_PCIE_RC_TYPE:
442		return rcar_gen4_add_dw_pcie_rp(rcar);
443	case DW_PCIE_EP_TYPE:
444		return rcar_gen4_add_dw_pcie_ep(rcar);
445	default:
446		return -EINVAL;
447	}
448}
449
450static int rcar_gen4_pcie_probe(struct platform_device *pdev)
451{
452	struct rcar_gen4_pcie *rcar;
453	int err;
454
455	rcar = rcar_gen4_pcie_alloc(pdev);
456	if (IS_ERR(rcar))
457		return PTR_ERR(rcar);
458
459	err = rcar_gen4_pcie_get_resources(rcar);
460	if (err)
461		return err;
462
463	err = rcar_gen4_pcie_prepare(rcar);
464	if (err)
465		return err;
466
467	err = rcar_gen4_add_dw_pcie(rcar);
468	if (err)
469		goto err_unprepare;
470
471	return 0;
472
473err_unprepare:
474	rcar_gen4_pcie_unprepare(rcar);
475
476	return err;
477}
478
479static void rcar_gen4_remove_dw_pcie(struct rcar_gen4_pcie *rcar)
480{
481	switch (rcar->mode) {
482	case DW_PCIE_RC_TYPE:
483		rcar_gen4_remove_dw_pcie_rp(rcar);
484		break;
485	case DW_PCIE_EP_TYPE:
486		rcar_gen4_remove_dw_pcie_ep(rcar);
487		break;
488	default:
489		break;
490	}
491}
492
493static void rcar_gen4_pcie_remove(struct platform_device *pdev)
494{
495	struct rcar_gen4_pcie *rcar = platform_get_drvdata(pdev);
496
497	rcar_gen4_remove_dw_pcie(rcar);
498	rcar_gen4_pcie_unprepare(rcar);
499}
500
501static const struct of_device_id rcar_gen4_pcie_of_match[] = {
502	{
503		.compatible = "renesas,rcar-gen4-pcie",
504		.data = (void *)DW_PCIE_RC_TYPE,
505	},
506	{
507		.compatible = "renesas,rcar-gen4-pcie-ep",
508		.data = (void *)DW_PCIE_EP_TYPE,
509	},
510	{},
511};
512MODULE_DEVICE_TABLE(of, rcar_gen4_pcie_of_match);
513
514static struct platform_driver rcar_gen4_pcie_driver = {
515	.driver = {
516		.name = "pcie-rcar-gen4",
517		.of_match_table = rcar_gen4_pcie_of_match,
518		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
519	},
520	.probe = rcar_gen4_pcie_probe,
521	.remove_new = rcar_gen4_pcie_remove,
522};
523module_platform_driver(rcar_gen4_pcie_driver);
524
525MODULE_DESCRIPTION("Renesas R-Car Gen4 PCIe controller driver");
526MODULE_LICENSE("GPL");